Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Commented Unassigned: MAJOR Issue with MVC 5.1/ Web API + Jquery [1664]

$
0
0
As of the 5.1 update you've introduced a serious issue in to Request.CreateResponse.

If you return Request.CreateResponse(HttpStatusCode.OK) (or Accepted or any other success code) and the jquery ajax call is setup something like this:

```
$.ajax({
type: "PUT",
url: url,
dataType: "json",
data: value == null ? null : JSON.stringify(value),
contentType: "application/json; charset=utf-8",
```

even though Request.CreateResponse is returning OK it will fail.

And before you say don't set the dataType to json if it isn't expecting a json response, lots of us have a lot of code that is generic for calling web api, and this isn't feesible because it would then have to know if it should be getting a value response back or not.

Web API used to return "no content" in the headers in the above CreateResponse example with no value, as it should. It now is not.

I would classify this as a massively breaking change that needs to be fixed ASAP and a public stable point release produced to fix it.
Comments: Change your put to this: public HttpResponseMessage Put(int ID, [FromBody]string value) { return Request.CreateResponse(HttpStatusCode.Accepted; } Now call this with Jquery 2.1 like this: ``` $.ajax({ type: "PUT", url: url, dataType: "json", data: value == value, contentType: "application/json; charset=utf-8", }).done((result) => { alert(fail); }).error((err) => { alert("fail"); }) ``` You'll notice that even though it returned accepted that jquery fails. Until 5.1 + jquery 2.1 this worked fine and got a success and went into the "done". For now as a work around I created a message handler that does the following which seems to work for anyone else that runs into this: ``` public class HttpResponseMessageHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { var response = await base.SendAsync(request, cancellationToken); if (response.Content == null && request.Headers.Accept.Any(a => a.MediaType == "application/json")) { var content = new StringContent("{}"); content.Headers.ContentLength = 2; content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); response.Content = content; } return response; } } ``` Just register it in your global.asax in the MessageHandlers and it's good. Ugly, but good.

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>