jQuery Ajax请求仅在Internet Explorer中返回null JSON

时间:2011-03-21 15:21:00

标签: jquery ajax internet-explorer asp.net-mvc-2 jquery-plugins

我正在使用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>");
}

2 个答案:

答案 0 :(得分:2)

您应检查您的HTML页面是否没有错误且没有警告,并将ajax配置设置为:

$.ajaxSetup({ cache: false });

答案 1 :(得分:0)

您是否尝试在选项中设置cache: false?默认情况下,IE缓存GET请求,这是.ajax()使用的,除非另有说明。这可能会导致您的问题。

编辑:

您是否尝试将ContentType更改为application/json?像这样:

public override void ExecuteResult(ControllerContext context)
{
    this.ContentType = "application/json";
    context.HttpContext.Response.Write("<textarea>");
    base.ExecuteResult(context);
    context.HttpContext.Response.Write("</textarea>");
}