Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC - Jquery $.ajax error callback is not returning responseJSON

I have the code below, with works fine on the development machine but not when called from a remote browser...

$.ajax({
  type: 'POST',
  url: '@Url.Action("Action", "Controller")',
  data: { id: id },
  dataType: 'json',
  async: true,
  success: function (data) {
  },
  error: function (jqXHR, status, err) {
    var result = jqXHR.responseJSON;
  }
});

The jqXHR.responseJSON object works when calling from localhost but not when the call is made from a remote computer, it's returned as undefined... Could somebody help me? Thanks in advance!

enter image description here

like image 717
Junior Silva Avatar asked Aug 29 '16 18:08

Junior Silva


2 Answers

I have been having sort of the same issue. When testing on my local machine using localhost, the ResponseJSON is filled. When I upload my project to the test server and test, my responseJSON is undefined and the responseTEXT is just giving the statusdescription that I sent:
(this is a JSONResult but I've done it as ActionResult)

Response.StatusCode = 400;
Response.StatusDescription = "Bad Request - Model State is Invalid";
return Json(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ModelState.Values.SelectMany(v => v.Errors)), JsonRequestBehavior.AllowGet);

and for ActionResult specifically I also tried:

return new HttpStatusCodeResult(400, new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ModelState.Values.SelectMany(v => v.Errors)));

which gave me a bunch of errors.

I tried with JsonResult the following code, which WORKED locally (but I had a syntax error in the JSON) but did not work on the test server.

return Json(new { success = false, responseJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ModelState.Values.SelectMany(v => v.Errors)) }, JsonRequestBehavior.AllowGet);

This is a recent test (above) when I used an AJAX request to submit a form model to an MVC Controller. I read that it's because it's going cross-domain but that makes no sense.

I created a web API controller and put the method information in there. This is because I wanted to use HttpReponseMessage and i figured that was a WebAPI type. After getting the model and the json and everything to work together, I tested and guess what?

When i use the HttpResponseMessage as the return type in WebAPI and return like this:

return Request.CreateResponse(HttpStatusCode.BadRequest, new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ModelState.Values.SelectMany(v => v.Errors)));

The responseJSON and responseText both fill correctly.

I don't know why it doesn't work otherwise and if someone has a response for that please tell us.

To me this is a work around because I feel like it should've worked the first way.

UPDATE: Using the ORIGINAL code with the MVC Controller- and adding

Response.TrySkipIisCustomErrors = true;

made the responseJSON appear.

like image 193
nebulous Avatar answered Oct 26 '22 03:10

nebulous


As suggested by nebulous, Response.TrySkipIisCustomErrors = true; seems to be the right way.

Unfortunately, most of the time IIS will ignore this property, and skip it (maybe that's why the developers call it Try...).

However, I fixed the trouble adding this on the Web.config:

<system.webServer>
  <httpErrors existingResponse="PassThrough" />
</system.webServer>

which is cleaner and doesn't "dirty" the code part.

Hope it helps.

like image 39
markzzz Avatar answered Oct 26 '22 05:10

markzzz