jQuery Ajax 请求仅在 Internet Explorer 中返回 null JSON
我正在使用 jQuery Form 插件向 ASP.NET MVC 控制器发出 jQuery Ajax 请求。
该调用工作正常,但是当我解析预期的 JSON 时,我在 Firefox 中得到了预期的结果,但在 Internet Explorer 中得到了 null。
Ajax 调用如下:
var options = {
iframe: true,
dataType: 'json',
success: function (result, status) {
$.unblockUI();
_editingEmail = false;
if (result.Sent === true) {
... Do something
}
$("#messageSentResult").html("<div>" + result.Message + "</div>");
},
error: function (xhr, textStatus, errorThrown) {
$.unblockUI();
alert(textStatus);
},
beforeSubmit: function () {
$.blockUI({
message: '<h1>Processing...</h1>'
});
}
};
$('#myForm').ajaxForm(options);
这是我的控制器:
[HttpPost]
public FileUploadJsonResult MyMethod()
{
... Do something
if(ValidationFails())
{
return new FileUploadJsonResult { Data = new { Sent = false, Message = "The operation was not successful." } };
}
return new FileUploadJsonResult { Data = new { Sent = true, Message = "The operation succeeded." } };
}
FileUploadJsonResult 类如下所示:
public override void ExecuteResult(ControllerContext context)
{
this.ContentType = "text/html";
context.HttpContext.Response.Write("<textarea>");
base.ExecuteResult(context);
context.HttpContext.Response.Write("</textarea>");
}
I am making a jQuery Ajax request to an ASP.NET MVC controller using the jQuery Form plugin.
The call works fine, but when I'm parsing the expected JSON I get the expected result in Firefox, but I get null in Internet Explorer.
The Ajax call is like this:
var options = {
iframe: true,
dataType: 'json',
success: function (result, status) {
$.unblockUI();
_editingEmail = false;
if (result.Sent === true) {
... Do something
}
$("#messageSentResult").html("<div>" + result.Message + "</div>");
},
error: function (xhr, textStatus, errorThrown) {
$.unblockUI();
alert(textStatus);
},
beforeSubmit: function () {
$.blockUI({
message: '<h1>Processing...</h1>'
});
}
};
$('#myForm').ajaxForm(options);
This is my controller:
[HttpPost]
public FileUploadJsonResult MyMethod()
{
... Do something
if(ValidationFails())
{
return new FileUploadJsonResult { Data = new { Sent = false, Message = "The operation was not successful." } };
}
return new FileUploadJsonResult { Data = new { Sent = true, Message = "The operation succeeded." } };
}
The FileUploadJsonResult class looks like this:
public override void ExecuteResult(ControllerContext context)
{
this.ContentType = "text/html";
context.HttpContext.Response.Write("<textarea>");
base.ExecuteResult(context);
context.HttpContext.Response.Write("</textarea>");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该检查您的 HTML 页面是否没有错误和警告,并将 ajax 配置设置为:
You should check that your HTML page has no errors and no warnings and set the ajax configuration to:
您是否尝试过在选项中设置
cache: false
?默认情况下,IE 会缓存 GET 请求,这就是.ajax()
使用的,除非另有说明。这可能会导致您的问题。编辑:
您是否尝试将 ContentType 更改为
application/json
?就像这样:Have you tried setting
cache: false
in your options? By default, IE caches GET requests, which is what.ajax()
uses, unless otherwise specified. This could be causing your problem.EDIT:
Have you tried changing the ContentType to
application/json
? Like so: