繁体   English   中英

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 上测试:成功和完整的功能都有效。

在 Chrome 49 上:成功工作,完成被调用,但 xhr.status 为 0 并且 xhr.responseText 为空。

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

尝试了在 SOF 上已经回答的问题,例如删除额外的逗号、添加 dataType ......但在 IE6 上仍然没有成功。

我们怎样才能做到一劳永逸?

谢谢

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

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

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

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

您可以参考以下代码来使用 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
    }
});

此外,请查看这篇文章

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM