IE6 Ajax 与 JQuery

IE6 Ajax with JQuery

我正在尝试从 REST 网络服务中提取数据到 HTML 页面中。问题是我正在努力工作的 Internet Explorer 6(这是我在 XP SP3 上的目标站)。 这是使用的代码:

$.ajax({
       type: "GET",
       contentType:"application/json; charset=utf-8",
       dataType : 'json',
       url: "https://jsonplaceholder.typicode.com/posts/1",
       success: function(data) {
            alert(data);
       },
       complete: function(xhr) {
          alert(xhr.status+" "+xhr.responseText);
       }
});

在 Firefox 52 ESR 上测试:成功和完整的功能都有效。

On Chrome 49:成功,调用完成,但 xhr.status 为 0,xhr.responseText 为空。

在 IE6 上根本不调用 success 并调用 complete 但 xhr.status 为 0 且 xhr.responseText 未定义。

尝试了 SOF 上已经回答的问题,例如删除多余的逗号、添加数据类型...但在 IE6 上仍然没有成功。

我们如何才能做到一劳永逸?

谢谢

IE6 是古老的,它不支持 CORS(甚至 XDomainRequest)。

无法在 IE6 中使用 JavaScript 执行跨源 RESTful HTTP 请求。

如果您想执行跨源请求,则需要使用其他一些(非RESTful)方法,例如 JSONP。

正如 Quentin 所说,IE 6/7 不支持发送 CORS 请求,您可以查看 this blog

您可以参考以下代码use JSONP

// Using JSONP
$.ajax({
    url: "<request url>",
    jsonp: "callback",

    // Tell jQuery we're expecting JSONP
    dataType: "jsonp",
    data: {
        q: "select title,abstract,url from search.news where query=\"cat\"",
        format: "json"
    },

    // Work with the response
    success: function( response ) {
        console.log( response ); // server response
    }
});

此外,请检查this article

Internet Explorer 9 及更早版本忽略 Access-Control-Allow headers 并且默认情况下禁止 cross-origin 对 Internet 区域的请求。要启用 cross-origin 访问权限,请转到“工具”->“Internet 选项”->“安全”选项卡,单击“自定义级别”按钮。找到杂项 -> 跨域访问数据源设置和 select“启用”选项。