简体   繁体   中英

Cross domain Ajax Call Not working in Safari 4 and Safari 5

I have the following jquery ajax code that is working in ALL other browsers except Safari 4 and Safari 5 in Mac. It works correctly in Safari 6.

$.ajax({
        type : "POST",
        url : crossDomainURL,
        contentType : "text/plain",
        data : requestData,
        dataType : "text",
        success : function(e) {
            console.log("success");

        },
        error : function(e) {
            console.dir(e);
            console.log("FAILLLLLLLLL");
        },
    });

It prints the "FAILLLLLLL" statement on the console everytime with a statusText of error and status of 0. The server at crossDomainURL is correctly configured to handle this type of request (and thus it works correctly in all other browsers).

But in case of Safari (4 and 5) on a Mac, the ajax call does not even leave the browser. It seems related to CORS issue, but I am confused as to why this same code works in Firefox and Chrome and Safari 6 correctly but not in Safari 4 & 5.

I appreciate any ideas.

Thanks.

EDIT: Older versions of Safari can be found here: http://michelf.ca/projects/multi-safari/

I put together an example nodejs application with the CORS headers set and am successfully able to complete a cross domain request on Safari 4 (Mac).

Access-Control-Allow-Origin *
Access-Control-Allow-Methods GET,PUT,POST,DELETE
Access-Control-Allow-Headers Content-Type

Server code can be found here: https://github.com/sfarthin/cross-domain-ajax-nodejs/blob/master/server.js and deployed onto Heroku (polar-dawn-3847.herokuapp.com).

Example cross domain request can be found here: http://jsfiddle.net/78SYj/2/ (jsFiddle's interface is broken in Safari 4, but still executes the ajax request)

$.ajax({
    type : "POST",
    url : "http://polar-dawn-3847.herokuapp.com/",
    contentType : "text/plain",
    dataType : "text",
    success : function(e) {
        alert("success");

    },
    error : function(e) {
        alert("FAILLLLLLLLL");
    }
});

If you share your server code/configuration, I think we'll be able to track down the issue.

I've read about bugs in the pre-6 versions of Safari which causes issues with Windows Authentication. One thing to try would be changing the authentication settings of your website. Remove Negotiate from providers, and leave NTLM.

More info: http://msdn.microsoft.com/en-us/library/cc339532%28v=vs.90%29.aspx

As it is working on all other browser but Safari 4 and 5 are giving you pain, I suspect it to be some configuration issue that doesn't go well with those two browsers.

CORS is supported by all major browser including those two you mentioned. Please check here

As Content Type is different than normal, you may want to include

Access-Control-Allow-Headers

for response header if it is missing.

Please check the request in fiddler to see what is being passed and why is it failing? If it is sending OPTION request first to seek permission from the server that whether it is allowed to make cross origin request, you may want to provide response with headers

Access-control-allow-origin  
Access-Control-Allow-Headers

even before your main request gets executed.

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