【问题标题】:Ajax GET request not working in IE 8 and 9Ajax GET 请求在 IE 8 和 9 中不起作用
【发布时间】:2013-09-18 15:50:18
【问题描述】:

我在阳光下尝试了所有方法,但无法正常工作。

尝试在 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.iecors) .. 仍然没有骰子

<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>

【问题讨论】:

  • JQuery ajax cross domain 的可能重复项 - 注意 jQuery.support.cors = true;
  • 为什么不使用jsonp 作为数据类型的常规ajax 调用?
  • 我会尝试的.. 抱歉,我的跨域经验很低。但似乎我在 IE8+9 中不支持 ajax,我必须使用 XDomain。
  • 使用 jsonp 数据类型获取解析器错误。还尝试了 Ajax 传输 github.com/dkastner/jquery.iecors

标签: jquery ajax internet-explorer-8 get xdomainrequest


【解决方案1】:

试试下面的代码,这是基本示例,我已经测试过所有浏览器都可以正常工作。

<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"));
?>

我认为这篇文章对你有帮助click here to view script

【讨论】:

  • 不幸的是,这不会跨域工作。我的要求是在 IE8 中跨域工作 .. 还应该提到需要建立一个 cookie 会话(我可以使用同域登录来完成)但是一旦我跨域我需要制作一堆 API调用 (GET) 它在 IE8+9 中中断。我的预感是 XDomain 不支持跨域 cookie 会话,但 Ajax(不是 IE8)支持跨域 cookie 会话。所以可能没有桨。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 2010-11-10
  • 2013-03-03
  • 1970-01-01
相关资源
最近更新 更多