【问题标题】:$.ajax call error in IE but not FireFoxIE 中的 $.ajax 调用错误,但不是 FireFox
【发布时间】:2009-07-06 17:27:28
【问题描述】:

出于调试目的,我正在尝试使用 $.ajax 而不是 getJSON。因为 getJSON 在 IE(6,7 或 8)中没有报错,我想弄清楚为什么一个 jQuery 插件没有将我返回的图像在 IE 中绘制到屏幕上,而是在其他浏览器中。

所以我尝试了这个。有趣的是,它在 IE 中遇到了错误事件,但在 firefox、safari 等中却没有,我不知道为什么(这段代码运行良好,在 FireFox 和其他部分中可以很好地呈现我的数据)。我知道我返回的 json 是有效的:

$.ajax({
    type: "GET",
    url: "http://localhost:59396/sss/sssHandler.ashx?action=getproducts&ids=" + ids,
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        alert(data.length);
        carousel.size(allProductIDs.length);

        if (numberOfImagesLeftToShow < numberOfImagesToDisplay) {
            first += (numberOfImagesToDisplay - numberOfImagesLeftToShow);
        }

        var d = 0;
        for (var i = first; i <= last; i++) {

            if (d != undefined) {
                // add data using index of the array returned by JSON (which starts at 0)
                carousel.add(i, decode(data[d].ImageTag));
            }

            // set to last ProductID showing in Carousel
            if (i == last) { lastProductID = parseFloat(data[d].ProductID); }

            d++;
        }
    },

    error: function() {
        alert("An error has occurred. Please try again.");
    }
});

我不知道还能做些什么来解决为什么 IE 在返回的 JSON 上遇到这么多问题,或者只是使用 getJSON 或 this 来执行函数(数据)。我也在响应中设置了标题而不是缓存。那不是问题。 问题是由于某种原因,IE 拒绝在响应中输入我的函数(数据)。

这是显示有效的返回 JSON(甚至使用 http://www.jsonlint.com/ 进行检查):

[
    {
        "ImageTag": "\u003cdiv class=\"CarouselItem
\"\u003e&lt;p&gt;&lt;img src=&quot;http://www.xxx.com/image/
2568.jpg&quot; alt=&quot;&quot;&gt;&lt;/p&gt;\u003cp\u003e\u003ca href=
\"Bear-10.prod\"\u003eTeddy Bear\u003c/a\u003e\u003c/p\u003e\u003cp
\u003e$20.95\u003c/p\u003e\u003cdiv\u003e",
        "ProductID": "540"
    },
    {
        "ImageTag": "\u003cdiv class=\"CarouselItem
\"\u003e&lt;p&gt;&lt;img src=&quot;http://www.xxx.com/image/
50.jpg&quot; alt=&quot;&quot;&gt;&lt;/p&gt;\u003cp\u003e\u003ca href=
\"Pendant362.prod\"\u003eBirthday\u003csup\u003e©\u003c/sup
\u003Necklace\u003c/a\u003e\u003c/p\u003e\u003cp\u003e$36.95\u003c/p
\u003e\u003cdiv\u003e",
        "ProductID": "83"
    }
] 

在我的 .ashx 中,我只需使用以下主要代码来生成响应

        context.Response.ContentType = "application/json";
        context.Response.Charset = Encoding.UTF8.ToString();
        context.Response.Cache.SetNoStore();
        context.Response.Cache.SetExpires(DateTime.MinValue);
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Cache.SetValidUntilExpires(false);

        context.Response.Expires = -1;
        context.Response.ExpiresAbsolute = DateTime.MinValue;
        context.Response.AddHeader("Cache-Control", "no-cache");
        context.Response.AddHeader("Cache-Control", "post-check=0, pre-check=0");
        context.Response.AddHeader("Pragma", "no-cache");

...

string jsonString = imageList.ToJSON(); <-- uses the .NET 3.5 JavaScriptSerializer
        context.Response.Write(jsonString);

此外,将 response.ContentType 指定为“application/json”还是“text/plain”似乎并不重要,因为至少我在 FireFox 中得到了返回和解析的有效数据。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    我怀疑它是 json 数据,让我们先尝试解决这个问题。如果您简单地返回,请求是否有效?

    [
        {
            "ImageTag": "imagePath",
            "ProductID": "540"
        },
        {
            "ImageTag": "imagePath",
            "ProductID": "83"
        }
    ]
    

    如果是这样,那么 ie 对某些编码有困难。

    注意,我还尝试了您在 jsonlint 处粘贴的 json,但无法验证它,除非我删除了第二张图片中 imageTag 的最后一行。

    你还真的需要在json中返回html吗?如果是这样,为什么不直接获取 html,因为您并没有真正从轻量级 json 结构中受益

    【讨论】:

    • 运行它。结果相同。在 FireFox 中运行良好,但在 IE8 中运行良好。它遇到错误:我得到 Line: 163 Error: System error: -1072896658。第 163 行是我的错误: function() { alert("发生错误。请重试。"); }
    • 实际上我确实想在其中返回 HTML,它只是 img 标签......没什么重的。
    • 因此,相同的代码在其他所有东西中运行是没有意义的,但 IE 和 IE 无法解析任何东西!
    • 我认为如果不使用 getJSON,你必须做一个 Eval,但我猜不是,它再次在 FireFox 中工作,我上面的代码。伙计,这令人沮丧。
    • 这一定是我不得不处理的最糟糕的情况......绝对是奇怪的
    【解决方案2】:

    不确定这是否与问题有关,但 contentType 参数指定了发送到服务器但未被接收的数据的 MIME 类型。 p>

    由于您没有发送任何内容(只是带有已在 URL 中指定的查询字符串的 GET),您可能不需要在选项中使用 contentType 或 data 参数。

    【讨论】:

      【解决方案3】:

      我认为 jQuery 使用 eval("(" + data + ")"),然后将结果传递给您的成功函数,因为您已指定您期待 JSON 数据。可能值得将此内容类型更改为“文本”,然后在您的成功回调中使用JSON.parse 来解析它。只是一个想法!

      【讨论】:

        【解决方案4】:

        修复它,

        已删除:

        context.Response.Charset = Encoding.UTF8.ToString();

        改变: context.Response.ContentType = "application/json;charset=utf-8";

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 2014-05-24
          • 1970-01-01
          • 2013-03-18
          • 1970-01-01
          • 1970-01-01
          • 2012-02-14
          相关资源
          最近更新 更多