現在、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)
IE8にはjsonサポートの問題があることがわかっているので、 json2.jsを追加するとこれが解決されると確信していました。json2 lib を追加しても、違いはないようです。また、これをヘッドに追加して、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>');
}