为什么这个简单的 jQuery getJSON 不能在 IE8 中工作?

发布于 2024-09-15 03:58:32 字数 1236 浏览 3 评论 0原文

我有一个非常标准的 AJAX 请求:

$.getJSON('/products/findmatching/38647.json', {}, function(JsonData){
  var tableHtml = '';
  var x;

  for (x in JsonData.matchingProds) {
    var matchingProd = JsonData.matchingProds[x];
    var buyMessage;

    if ( x == 0 ) {
      buyMessage = 'Buy Cheapest';
    }
    else {
      buyMessage = 'Buy from this shop';
    }

    tableHtml = tableHtml + '<tr><td><img height="40" src="' + matchingProd.img_url + '" alt="' + matchingProd.name + '" /></td> \
      <td><a href="' + matchingProd._page_url + '">' + matchingProd.name + '</a></td> \
      <td><a href="' + matchingProd._merchant._url + '">' + matchingProd._merchant.title + '</td> \
      <td align="right">&pound;' + matchingProd.price + '</td> \
      <td><a href="' + matchingProd.referral_url + '">' + buyMessage + '</a></td></tr>';
  }

  $('#matchingproducts tbody').html(tableHtml);

  $('#loading').delay(1000).fadeOut();
});

它在除 IE 之外的所有浏览器中都能正常工作。我不再用 IE 做太多事情,因为我有 Mac,但我在 XP 虚拟机上有 IE8。使用其内置的调试工具并没有真正帮助(它们不是很好)。我唯一能理解的是,在某些时候我收到“预期标识符”错误。

这可能是在返回的 JSON 数据中吗?我如何从 IE 的角度检查这些数据?

I've got a very standard AJAX request:

$.getJSON('/products/findmatching/38647.json', {}, function(JsonData){
  var tableHtml = '';
  var x;

  for (x in JsonData.matchingProds) {
    var matchingProd = JsonData.matchingProds[x];
    var buyMessage;

    if ( x == 0 ) {
      buyMessage = 'Buy Cheapest';
    }
    else {
      buyMessage = 'Buy from this shop';
    }

    tableHtml = tableHtml + '<tr><td><img height="40" src="' + matchingProd.img_url + '" alt="' + matchingProd.name + '" /></td> \
      <td><a href="' + matchingProd._page_url + '">' + matchingProd.name + '</a></td> \
      <td><a href="' + matchingProd._merchant._url + '">' + matchingProd._merchant.title + '</td> \
      <td align="right">£' + matchingProd.price + '</td> \
      <td><a href="' + matchingProd.referral_url + '">' + buyMessage + '</a></td></tr>';
  }

  $('#matchingproducts tbody').html(tableHtml);

  $('#loading').delay(1000).fadeOut();
});

It works fine in all browsers except IE. I don't do much in IE anymore as I have a Mac, but I've got IE8 on an XP virtual machine. Using its built-in Debug Tools hasn't really helped (they're not very good). The only thing I can fathom is that at some point I get and "Expected identifier" error.

Could this be in the JSON data that's returned? How can I examine that data from IE's point of view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

幸福%小乖 2024-09-22 03:58:33

嗯...看起来你的脚本在 IE 中运行良好。唯一出现问题的是您的 jQuery fadeOut 方法。我可以在这里找到相关内容:

jquery IE 淡入和淡出不透明度

基本上,IE 在更改之前未声明的 CSS 属性时会出现问题。

编辑:也许它在 IE 中运行得不好......我可能不明白页面加载的确切过程。

Hm...it appears that your script is running fine in IE. The only thing that appears to be breaking is your jQuery fadeOut method. I was able to find something about that here:

jquery IE Fadein and Fadeout Opacity

Basically, IE has issues with altering CSS properties when they haven't previously been declared.

Edit: Perhaps it is not running fine in IE...I might not have understood the exact process of the page load.

铁憨憨 2024-09-22 03:58:32

好吧,我想通了。有人建议尝试非缩小版本的 jQuery。我这样做并单步调试了 IE8s Javascript 调试器。在某个时刻,出现了以下错误:

Could not complete the operation due to error c00ce56e.

稍微谷歌搜索一下,发现这是我为 JSON 数据设置的字符集声明。在 PHP 中,这是通过以下方式完成的:

header ( 'Content-Type: text/javascript; charset=utf8' );

事实证明,IE 对于字符集引用非常特殊( http://forums.asp.net/t/1345268.aspx#2732852 ),所以我将其更改为:

header ( 'Content-Type: text/javascript; charset=UTF-8' );

嘿-急,它就像一个魅力。感谢你们的帮助,你们再次为我指明了正确的方向!

Ok I figured it out. Someone suggested trying a non-minified version of jQuery. I did this and stepped through the IE8s Javascript debugger. At a certain point, the following error came up:

Could not complete the operation due to error c00ce56e.

A little Googling found that it was the charset declaration I've set for my JSON data. In PHP, this was done with:

header ( 'Content-Type: text/javascript; charset=utf8' );

It turns out that IE is very particular about the charset reference ( http://forums.asp.net/t/1345268.aspx#2732852 ), so I changed it to:

header ( 'Content-Type: text/javascript; charset=UTF-8' );

And hey-presto, it works like a charm. Thanks for your help guys, you pointed me in the right direction again!

半世蒼涼 2024-09-22 03:58:32

再次编辑 - 仍在调试 - 更改为使用其他函数需要使最后一个参数为 myAjaxResponderFunc 且不带引号...

edit again — still debugging - that change to use the other function needs to have the last argument be myAjaxResponderFunc with no quotes ...

在你怀里撒娇 2024-09-22 03:58:32
$.ajaxSetup({ cache: false }); 
$.ajaxSetup({ cache: false }); 
薄情伤 2024-09-22 03:58:32

您必须使用检查浏览器和 IE8+ 版本,然后使用 XDomainRequest()(如果是 msie8+)。

这将返回一个 JSON 字符串,必须使用 jQuery.parseJSON() 来创建 JSON 对象...

不要使用 getJSON!

这是我的示例:

if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && window.XDomainRequest) {
        // Use Microsoft XDR
        var xdr = new XDomainRequest();
        xdr.open("get", reqURL);
        xdr.onload = function() {
            var json = xdr.responseText;
            json = $.parseJSON(json);

            $.each(json.results, function(i, val) {
                    console.log(val.formatted_address);
                    var locString = val.formatted_address;
                    $.each(val.address_components, function(x, comp) {

                        if($.inArray("postal_code", comp.types) > -1) {
                            //alert("___" + comp.types);
                            zipmap[locString] = comp.short_name;
                        }

                    });

                    suggestions.push(val.formatted_address);
                });

            //alert(json.results);
        }
        xdr.send();
        add(suggestions); 
    }else {
        $.getJSON(reqURL, function(data) {

            var isZIP = new Boolean;
            console.log(data.results);
            $.each(data.results, function(i, val) {
                console.log(val.formatted_address);
                var locString = val.formatted_address;
                $.each(val.address_components, function(x, comp) {

                    if($.inArray("postal_code", comp.types) > -1) {
                        console.log("___" + comp.types);
                        zipmap[locString] = comp.short_name;
                    }

                });

                suggestions.push(val.formatted_address);
            });

            add(suggestions);  

        });
    }

requrl 是您要向其发出请求的 URL。

完毕!

信用至:
http: //graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/

我就是喜欢 IE!

You have to use check browser and version for IE8+, then use the XDomainRequest() if msie8+.

This will return a JSON String, must use jQuery.parseJSON() to create the JSON object…

Don't use getJSON!

Here's my example:

if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && window.XDomainRequest) {
        // Use Microsoft XDR
        var xdr = new XDomainRequest();
        xdr.open("get", reqURL);
        xdr.onload = function() {
            var json = xdr.responseText;
            json = $.parseJSON(json);

            $.each(json.results, function(i, val) {
                    console.log(val.formatted_address);
                    var locString = val.formatted_address;
                    $.each(val.address_components, function(x, comp) {

                        if($.inArray("postal_code", comp.types) > -1) {
                            //alert("___" + comp.types);
                            zipmap[locString] = comp.short_name;
                        }

                    });

                    suggestions.push(val.formatted_address);
                });

            //alert(json.results);
        }
        xdr.send();
        add(suggestions); 
    }else {
        $.getJSON(reqURL, function(data) {

            var isZIP = new Boolean;
            console.log(data.results);
            $.each(data.results, function(i, val) {
                console.log(val.formatted_address);
                var locString = val.formatted_address;
                $.each(val.address_components, function(x, comp) {

                    if($.inArray("postal_code", comp.types) > -1) {
                        console.log("___" + comp.types);
                        zipmap[locString] = comp.short_name;
                    }

                });

                suggestions.push(val.formatted_address);
            });

            add(suggestions);  

        });
    }

requrl is the URL which you are making a request to.

Done!

Credit to:
http://graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/

I just LOVE IE!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文