Reputation: 3108
I'm using ASP.NET WebApi to build my REST services. In many cases, I only need to return a 200 OK message (with no body). The response typically looks like (it's a POST):
return this.Request.CreateResponse(HttpStatusCode.OK);
or
return new HttpResponseMessage(HttpStatusCode.OK);
Both worked well in jQuery 1.8, but that's because that version allowed a responseText of "" to be used (which it then attempts to run parseJSON on).
However, in jQuery 2.0, it ends up in the deferred.fail() callback instead, as it believes that a failed parseJSON (on empty text) is invalid..
How do I solve this?
UPDATE
Example JS call that uses this:
var deferred = $.ajax({
"type": "POST",
"url": something,
"contentType": "application/json; charset=utf-8",
"dataType": "json",
"data": JSON.stringify(whatever)
});
deferred.fail(function (msg) {
// it ends up in here
});
The call could result in a JSON response, but often it just needs to return 200 OK with no body, which is why I'm still specifying the dataType/contentType (there's no way to know beforehand if it will have a body or not, the server has the logic to decide that).
Upvotes: 3
Views: 1126
Reputation: 4063
Your dataType
is set to JSON
which means that you expect to get JSON back from the server.
You could try changing this to text
to see if that resolves the issue.
Or if it's not always empty, try leaving it out entirely as jQuery in theory should be able to recognize the format.
See the docs:
http://api.jquery.com/jQuery.ajax/
Upvotes: 4