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

发布于 2024-10-24 07:03:53 字数 1493 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花肆意 2024-10-31 07:03:53

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

$.ajaxSetup({ cache: false });

You should check that your HTML page has no errors and no warnings and set the ajax configuration to:

$.ajaxSetup({ cache: false });
我们只是彼此的过ke 2024-10-31 07:03:53

您是否尝试过在选项中设置 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>");
}

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:

public override void ExecuteResult(ControllerContext context)
{
    this.ContentType = "application/json";
    context.HttpContext.Response.Write("<textarea>");
    base.ExecuteResult(context);
    context.HttpContext.Response.Write("</textarea>");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文