Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Hello I've got a strange problem with an AJAX call on my site. I make a simple AJAX call to a script on my site. But the AJAX call fails with readyState = 4 and status = 0. There's no cross domain problem 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've googled a lot of sites but there seems to be no solution for that!

like image 260
Jay Avatar asked Oct 26 '22 12:10

Jay


1 Answers

This happens when the URL of the AJAX request has a different domain than the page the script is running in. For example, www.mydomain.com and mydomain.com are different.

To fix it, replace

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

with

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

I found this question while looking for the answer myself. I got the solution from here.

like image 149
imgx64 Avatar answered Dec 05 '22 06:12

imgx64