AJAX fails with HTTP status code 0

I want to load an HTML file into a textarea with a relative link. It was good when running locally, but it failed when I tried here.I set console.log(xmlhttp.readyState);console.log(xmlhttp.status); , it shows 1,0,4,0 respectively.

Oh, I found the problem, index .html can only be used/accessed, directly using index.html equivalent to accessing a non-existent file. But accessing non-existent files on my local server returns 404 instead of 0, why?

I don’t fully understand your troubleshooting. I can see some documentation regarding the readyState and status attributes. They all have one number, so that debug statement should have produced two numbers, not four.

Also, it would help a lot if we could see the page for ourselves, so we can check some thing instead of having to troubleshoot by going back and forth with you.

By default, our 404 behavior is to redirect to an error page hosted by us. So the request returns a 302 status code, not a 404. That seems like the most likely cause to me.

1 Like

Sorry, those 4 parameters are actually returned 2 times in succession. This is the page I am talking about: http://mlh-aids.42web.io/w/editor.php . Username 114514, Password 1919810. (This is a temporary account). It seems like the code is a little messy because I’ve tried quite a bit. I used a 404 to determine that the file does not exist at localhost, and tried to use timeout after finding that infinityfree did not work, but timeout behaved strangely, and I often need multiple requests to trigger timeout

I took a look at the editor page and I wasn’t really able to reproduce the issue myself.

Something did come to mind though.

When you test everything on localhost, AJAX requests are usually really, really fast because there is no network overhead. But if you need to do the request to a server on the other side of the planet, it’s going to be a lot slower, and this may cause problems with asynchronous scripts. So this difference in behavior may just be a normal consequence of going to a live server.

Also, if I were you, I wouldn’t use raw XHR. You already have jQuery loaded into the page through the Bootstrap bundle, and using the AJAX functions provided by jQuery may be easier than trying to build everything from scratch.

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.