I try to detect rather the page status is 404 or not by this code :
var request = new XMLHttpRequest();
request.open('GET', '.aspx', true);
request.send();
if (request.status === "404") {
alert("Oh no, it does not exist!");
}
and I get this strangiest error in my browser console :
Uncaught InvalidStateError: Failed to read the 'status' property from 'XMLHttpRequest': the object's state must not be OPENED.
any clue whats going on ?
I try to detect rather the page status is 404 or not by this code :
var request = new XMLHttpRequest();
request.open('GET', 'http://xxxxxxxx.me/yossi/default.aspx', true);
request.send();
if (request.status === "404") {
alert("Oh no, it does not exist!");
}
and I get this strangiest error in my browser console :
Uncaught InvalidStateError: Failed to read the 'status' property from 'XMLHttpRequest': the object's state must not be OPENED.
any clue whats going on ?
Share Improve this question edited Nov 19, 2013 at 17:14 thormayer asked Nov 19, 2013 at 17:07 thormayerthormayer 1,0706 gold badges29 silver badges49 bronze badges 2-
Can you wait for a
readystatechanged
? ie. add anonreadstatechanged
listener. – Halcyon Commented Nov 19, 2013 at 17:13 - it's onreadystatechange event handler and NOT onreadystatechanged .. – Navin Israni Commented Nov 29, 2013 at 7:37
2 Answers
Reset to default 2You're opening the connection as ASYNC request.open('GET', 'http://*****/yossi/default.aspx', true);
(the true
part of the request).
This means that the request.send();
call is made and immediately continues execution of your program. While your program is continuing to process the next lines of code, the XMLHttpRequest is doing its own work in the background and making the HTTP request and thus the connection is currently in an "Open" state when the following code is hit:
if (request.status === "404") {
alert("Oh no, it does not exist!");
}
You have two options
- Make the
request.send();
call synchronous (vs async). This is NOT RECOMMENDED as the browser's UI will freeze while the call is being made until your request returns. - You need to add a callback for when the requests'
state
has changed. This is done with anonreadystatechange
property off of yourrequest
variable. Within the callback, you can check the status because it is not until the request has been pleted that you would even be able to know if the status was "404" or not (or any other status for that matter). An example taken from http://www.w3schools./ajax/ajax_xmlhttprequest_onreadystatechange.asp
Example of async call with onreadystatechange
callback:
request.onreadystatechange = function() {
if (request.readyState==4 && xmlhttp.status==404) {
alert("Oh no, it does not exist!");
}
}
Make your if block like this:
if (request.readyState == 4 && request.status === 404) { ... }
You're not waiting until the page finishes loading. You're using an asynchronous request, but the way you are handling it makes it synchronous. If I were you I'd look up asynchronous AJAX or try using JQuery.
https://developer.mozilla/en-US/docs/Web/API/XMLHttpRequest
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745354511a4624035.html
评论列表(0条)