Why isn't this simple bit of jQuery getJSON working in IE8?

后端 未结 5 651
时光说笑
时光说笑 2021-01-03 03:03

I\'ve got a very standard AJAX request:

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

  for (x i         


        
5条回答
  •  孤城傲影
    2021-01-03 03:23

    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!

提交回复
热议问题