1

私は太陽の下ですべてを試しましたが、これを機能させることはできません。

IE 8 + 9 でドメインをまたがる単純な GET リクエストを取得しようとすると、Chrome と Firefox と IE10 で正常に動作します。XDomainRequest を使用しようとしましたが、サイコロはありません.. 未定義のエラーが発生します。

function RequestWrapper (url, data, error, success, method) {

// IE8 & 9 only Cross domain JSON GET request
if ('XDomainRequest' in window && window.XDomainRequest !== null) {

    var xdr = new XDomainRequest(); // Use Microsoft XDR

    xdr.open(method, url);

    xdr.onload = function () {
        var dom  = new ActiveXObject('Microsoft.XMLDOM'),
            JSON = $.parseJSON(xdr.responseText);

        dom.async = false;  // I've tried both true and false

        if (JSON == null || typeof (JSON) == 'undefined') {
            JSON = $.parseJSON(data.firstChild.textContent);
        }

        success(JSON);
    };

    xdr.onerror = function () {

        error();
    }

    xdr.send();

} 

// Do normal jQuery AJAX for everything else          
else {

    $.ajax({
        url: url,
        type: method,
        data: data,
        dataType: 'json',
        success: success,
        error: error,
        xhrFields: { withCredentials: true }
    });

}

}

RequestWrapper(

// URL
'http://myURL',

// Data
null, 

// error
function (xhr, status, error) { console.log('error: ' + status); },

// success
function (data) { console.log('success: ' + data); },

// method
'get'

);

編集: jsonp を使用してみましたが、パーサー エラーが発生します。iecors.js jQuery ajax カスタム トランスポート ( https://github.com/dkastner/jquery.ieco ​​rs ) も試してみました .. まだサイコロはありません

<script src="jquery.iecors.js"></script>
<script type="text/javascript">

function RequestWrapper (url, data, error, success, method) {

$.ajax({
    url: url,
    type: method,
    data: data,
    dataType: 'jsonp',
    success: success,
    error: error,
    xhrFields: { withCredentials: true }
});
}

RequestWrapper(

// URL
'http://givingimages.pixfizz.com/v1/users/1891548/books.json',

// Data
null, 

// error
function (xhr, status, error) { console.log('error: ' + status); },

// success
function (data) { console.log('success: ' + data); },

// method
'get'

);


</script>
4

1 に答える 1

0

以下のコードを試してみてください。これは基本的な例であり、すべてのブラウザで既にテスト済みです。

<html>
<head>
<!-- Include jquery from Google here -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<script  type="text/javascript">
// Wait till dom is loaded
$(document).ready(function() {

 // Load ajax.php as JSON and assign to the data variable
 $.getJSON('ajax.php', function(data) {
    // set the html content of the id myThing to the value contained in data
    $("#myThing").html(data.value);
 });   
});
</script>
</head>
 <body>
  <p id="myThing"></p>
</body>
</html>

ajax.php ファイルを作成する

<?php
echo json_encode(array("value" => "Hello World"));
?>

この投稿が役に立つと思います ここをクリックしてスクリプトを表示

于 2013-09-14T04:21:05.140 に答える