【问题标题】:Getting parserror using Jquery+ASP.Net MVC on Firefox在 Firefox 上使用 Jquery+ASP.Net MVC 获取解析错误
【发布时间】:2009-09-19 18:50:42
【问题描述】:

我有一个简单的 $.ajax 请求,我试图在我的 ASP.Net MVC 应用程序中获取一些 HTML 内容。

        // Load the claim table
    function GetClaimTable() {
        $.ajax({
            type: "GET",
            url: "claimtable",
            data: {},
            datafilter: null,
            dataType:'text',
            success: function(msg){
                alert(msg);
                $("#claimTable").html(msg.responseText);
            },
            error: function(msg, sdf, sdd) {
                alert(sdf);
                alert(sdd);
            }
        });

但是我得到了一个解析错误。调用成功是因为我在 firefox 中看到 200 OK 并且错误有 XmlHttpRequest 对象,该对象在 responseText 属性中具有正确的数据。

该代码在 IE 中运行良好,但在 Firefox 中失败。 url 声明表是一个简单的 MVC 操作。

我在这里读到jQuery / ASP MVC -- parsererror in "$.ajax" calls 这是由于在 jquery 1.3.2 中解决的拼写错误。但是我有 1.3.2,我收到了这个错误。

有什么帮助吗?

【问题讨论】:

  • 你能贴出FF给你的完整解析错误吗
  • 没有完整的解析器错误。这就是全部。这是一个字符串:“parsererror”

标签: javascript jquery asp.net-mvc firefox


【解决方案1】:

如果您尝试从服务器获取 HTML,为什么要指定 dataType:'text'?您的操作发回的 ContentType 标头是什么?看起来服务器发送的 ContentType 标头与实际内容之间存在一些不一致。

【讨论】:

  • 我也尝试过 contentType HTML。我最终使用了 text ,因为那是一种不应该被解析的 contentType。
【解决方案2】:

我终于知道为什么会这样了。原因是我编写的 ajaxSetup 可以更顺畅地处理我的 jQuery Web 服务请求。显然,即使我覆盖了新函数中的设置并且发生了解析器错误,也出现了问题。我删除了 ajaxSetup,现在一切正常。

这是毁掉我三个小时生命的函数。

$.ajaxSetup({
    type: "POST",
    cache:false,
    contentType:"application/json;charset=utf-8",
    data:"{}",
    dataFilter: function(data) {
        var msg;

        if (typeof (JSON) !== 'undefined' && 
            typeof (JSON.parse) === 'function')
          msg = JSON.parse(data);
        else
          msg = eval('(' + data + ')');

        if (msg.hasOwnProperty('d'))
          return msg.d;
        else
          return msg;
        }
});

看起来很无辜。嗯?

【讨论】:

    【解决方案3】:

    无需执行 msg.responseText。 msg 本身就是 responseText

    【讨论】:

    • 嗯...就像我说的请求失败了。在错误函数 msg 中有一个 responseText 属性。如果它成功了,它就不会拥有它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 2014-01-28
    • 2011-03-31
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多