1

我正在处理我们现在接管的一些第 3 方代码,这些代码在 IE8 中不起作用。这只是一个返回一些 json 数据的 ajax 调用。在大多数浏览器中都可以正常工作,但 IE8 会引发语法错误。根据 IE8 开发工具脚本调试器,这是有问题的代码(来自 jquery 1.9.1.min):

return e.JSON&&e.JSON.parse?e.JSON.parse(n):null===n?n:"string"==typeof n&&(n=b.trim(n),n&&k.test(n.replace(S,"@").replace(A,"]").replace(E,"")))?Function("return "+n)():(b.error("Invalid JSON: "+n),t)

我确信添加json2.js会解决这个问题,因为我知道 IE8 有一些 json 支持问题。添加 json2 库似乎没有任何区别。我还通过将其添加到头部来确保 IE8 未处于兼容模式:

<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />

因为我很确定这也会导致一些 json 问题。

如果这很重要,项目正在使用jquery 1.9.1,但我认为它不会解决这个问题。我很确定 1.9 放弃了对 $.browser 的支持,但它看起来不像在这个代码库中使用。

返回的 json 也是有效的。

有人有想法么?


编辑 这是代码。第一个警报在 IE8 中未定义。

Geocode = function (address, state) {
var url = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=';
var param = encodeURIComponent(address + ', ' + state);

var result = jQuery.ajax({
    url: url + param,
    type: "GET",
    async: false,
    dataType: 'json'
});

alert(result.responseText); //undefined in ie8
var json = jQuery.parseJSON(result.responseText);
alert(json.results[0].geometry.location.lat);

if (json.status != 'OK') {
    alert('Unable to determine the location of the city and state you entered');
    return null;
}

return json.results[0].geometry.location;

};

编辑 2看起来问题是 IE8-9 对跨域请求的部分支持。以下是我昨晚玩过的一些精简代码,我的请求成功了。代码与上面的不匹配,但基本上你需要使用 XDomainRequest:

var protocol = location.protocol,
    url = '//maps.googleapis.com/maps/api/geocode/json?sensor=false&address=',
    city = 'san francisco',
    state = 'california',
    address = encodeURIComponent(city + ', ' + state);

/**********************************************************

for cross domain requests in ie8-9 (partial CORS support), use special XDR (XDomainRequest), but it does have limitations
http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

    1. The target URL must be accessed using the HTTP or HTTPS protocols
    2. The target URL must be accessed using only the HTTP methods GET and POST
    3. No custom headers may be added to the request
    4. Only text/plain is supported for the request's Content-Type header
    5. No authentication or cookies will be sent with the request
    6. Requests targeted to Intranet URLs may only be made from the Intranet Zone
    7. Requests must be targeted to the same scheme as the hosting page

***********************************************************/
if ('XDomainRequest' in window && window.XDomainRequest !== null) {
    //IEs that do not support cross domain xhr requests
    var xdr = new XDomainRequest();

    xdr.open('get', protocol + url + address);
    xdr.onload = function() {
        var data = $.parseJSON(xdr.responseText);
        outputData(data);
    };
    xdr.send();
} else {
    //good browsers
    $.ajax({
        url: protocol + url + address,
        type: 'get',
        dataType: 'json',
        success: function(data){
            outputData(data);
        }
    });
}

function outputData(data) {
    $('body')
        .append('<p>status: ' + data.status + '</p>')
        .append('<p>latitude: ' + data.results[0].geometry.location.lat + '</p>');
}
4

0 回答 0