XHR readyState = 4, but Status = 0 in Google Chrome

Hi, I have a strange problem with calling AJAX on my site. I am making a simple AJAX script call on my site. But the AJAX call fails with readyState = 4 and status = 0. There is no problem with the cross domain, because the script I want to call is on my server.

$.ajax({
                    type:"GET",
                    url: 'http://mydomain.com/test.php',
                    success : function(response){
                        console.log(response);
                    },
                    error : function(XHR){
                        console.log(arguments);
                    }
});

I searched a lot of sites, but there seems to be no solution!

+5
source share
1 answer

This happens when the AJAX request URL has a different domain than the script page. For example, www.mydomain.comthey mydomain.comare different.

To fix this, replace

url: 'http://mydomain.com/test.php',

with

url: 'http://' + document.domain + '/test.php',

, . .

+3

All Articles