我向来的观点,IE就是个奇葩。
服务器返回json,chrome处理得好地地,但IE却奇葩地向你请求是否要保存这个JSON文件?
之所以出现这种弱智现象,是因为IE无法识别一个所谓的响应头部:application/json
那如何处理?这样子:
服务器端:
public ContentResult OperateResult(bool ok = true,string msg = "保存成功")
{
return new ContentResult
{
ContentEncoding = Encoding.GetEncoding("utf-8"),
//ContentType = "application/json",//IE说它不认识
ContentType = "text/plain;charset=UTF-8",
Content = $@"{{""ok"" : ""{ok.ToString().ToLower()}"",""message"" : ""{msg}""}}"
};
}
前端:
$('#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);
}
});
其他什么在服务器ContentType = "text/html"
,然后客户端 $.parseJSON(xhr.responseText);
都是扯蛋。