简体   繁体   中英

IE6 Ajax with JQuery

I'm trying to fecth data from a REST webservice into a HTML page. The problem is Internet Explorer 6 (which is my target station on XP SP3) that I'm struggling to make work. Here's the code used:

$.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);
       }
});

Tested on Firefox 52 ESR: both success and complete functions works.

On Chrome 49: success works, complete is called but xhr.status is 0 and xhr.responseText is empty.

On IE6 success is not called at all and complete is called but xhr.status is 0 and xhr.responseText is undefined.

Tried what was already answered here on SOF like removing extra commas, adding dataType... but still no success with IE6.

How can we do it once for all?

Thanks

IE6 is ancient , it does not support CORS (not even with XDomainRequest ).

There is no way to perform cross-origin RESTful HTTP requests with JavaScript in IE6.

If you want to perform a cross-origin request, then you will need to use some other (non-RESTful) approach such as JSONP.

As Quentin said, sending CORS request is not supported in IE 6/7, you could check this blog .

You could refer to the following code to 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
    }
});

Besides, please check this article :

Internet Explorer 9 and earlier ignores Access-Control-Allow headers and by default prohibits cross-origin requests for Internet Zone. To enable cross-origin access go to Tools->Internet Options->Security tab, click on “Custom Level” button. Find the Miscellaneous -> Access data sources across domains setting and select “Enable” option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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