Asp.net MVC - jQuery $ .ajax error callback does not return responseJSON

I have the code below that works fine on a 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 called from localhost, but not when the call is made from a remote computer, it returns as undefined ... Can someone help me? Thanks in advance!

enter image description here

+4
source share
1 answer

. localhost, ResponseJSON . , JSON - undefined, responseTEXT -, :
( JSONResult, 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);

ActionResult :

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

.

JsonResult , ( JSON), .

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

(), AJAX MVC. , - , .

- API . , HttpReponseMessage, , WebAPI. json , , , ?

HttpResponseMessage WebAPI :

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

JSON responseText .

, , - , , .

, , .

UPDATE: ORIGINAL MVC

Response.TrySkipIisCustomErrors = true;

JSON.

+6

Source: https://habr.com/ru/post/1652907/


All Articles