This project is read-only.

Approach to returning failure status codes to jquery ajax call?

Topics: jQuery
Jan 8, 2011 at 3:18 AM

I am wanting to return Http Status Ok or Failure to populate the jquery ajax call success/failure events using HttpStatusCodes.

I have this signature:

[WebInvoke(UriTemplate ="", Method="POST")]
        public JsonValue Post(JsonValue post, HttpResponseMessage message)

I Get the exception:
Operation 'Post' of contract 'DownloadResource' specifies at least one request body parameter in addition to the JsonValue parameter which is not mapped to the UriTemplate. All non-JsonValue parameters must be mapped to the UriTemplate.

Taking away the HttpResponseMessage works but I am unsure how to return failure responses to the client?
What is the approach to sending back HttpResponseMessage results to the client when using ServiceModel.Web.jQuery?

Jan 29, 2011 at 10:15 PM

The JsonValue / jQuery support is still not integrated with the new WCF HTTP framework (the integration should be live in a couple releases). Currently, if you want to change the return value using the jQuery support, you can throw a WebFaultException<JsonValue>. The WFE has a field for the status code, which will be used in the response:

JsonValue responseBody = CreateErrorResponseBody();
throw WebFaultException<JsonValue>(responseBody, HttpStatusCode.BadRequest).

Jan 29, 2011 at 10:46 PM

We do have support for JsonValue in the WCF HTTP bits if you download the source however it doesn't just work out of the box as nicely as in the jQuery bits and doesn't support anything other than JsonValue. It also does not have the templating / tooling support. We are working on fully consolidating the two.

If you take a look at the JsonValue sample in the WCF HTTP samples you will see using JsonValue with WCF HTTP. In order to set the status code, you can add an HttpResponseMessage parameter in the signature of the operation. Once you have the response you can set the Status code on it directly i.e.

public JsonValue Get(JsonValue value, HttpResponseMessage response) {