In MVC, why does returning Content sometimes fail in an Ajax callback, returning Json, even for simple string objects?
Even when this fails, the data is still available if you want to access it in the always callback ...
Update:
When I set contentType in an ajax call to text/xml , the response will no longer introduce an error message.
AJAX:
$.ajax({ cache: false, type: "GET", contentType: "application/json; charset=utf-8", dataType: 'json', url: "/MyController/GetFooString", data: { }, success: function (data) { alert(data); }, error: function (xhr, ajaxOptions, thrownError) { alert("Ajax Failed!!!"); } }); // end ajax call
Controller action that fails (sometimes)
Even when it fails, data is still available.
public ActionResult GetFooString() { String Foo = "This is my foo string."; return Content(Foo); } // end GetFooString
Controller action that always works
public ActionResult GetFooString() { String Foo = "This is my foo string."; return Json(Foo, JsonRequestBehavior.AllowGet); } // end GetFooString
json jquery c # ajax asp.net-mvc
silencedmessage
source share