In my opinion, IE is a freak.
The server returns json, Chrome handles it well, but Internet Explorer asks you to save the json file.
The reason for this retardation is that IE cannot recognize a so-called response header: Application/JSON
How to deal with that? Server side:
public ContentResult OperateResult(bool ok = true.string msg = "Saved successfully")
{
return new ContentResult
{
ContentEncoding = Encoding.GetEncoding("utf-8"),
//ContentType = "application/json",//IE says it doesn't know
ContentType = "text/plain; charset=UTF-8",
Content = $@"{{""ok"" : ""{ok.ToString().ToLower()}"",""message"" : ""{msg}""}}"
};
}
Copy the code
Front end:
$('#mainForm').ajaxSubmit(
url: "@url".dataType: 'json'.//<---------------
type: 'post'.success: function (data) {
if (data.ok == "true") {
toastr.success(data.message);
} else{ toastr.error(data.message); }},error: function (e) { toastr.error(e); }});Copy the code
What else is on the server ContentType = “text/ HTML “, then the client $.parsejson (xhr.responseText); It’s bullshit.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = in fact, on the server side (c #) writes, the same problems:
public ActionResult OperateResult(bool ok = true.string msg = "Saved successfully")
{
return Json(new {ok = ok,message = msg });
}
Copy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = in asp.net to WebApi, usually returned to the front in json format. On the server side, simply set the header type to JSON:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); // Change the mode to JSONCopy the code
Because IE recognizes “text/ HTML”, it does not have this problem. But because of the change in the return format, it is no longer possible to return to the page. But since it’s WebAPI, who wants a page?