javascript - Failed to read the 'status' property from 'XMLHttpRequest': the object's st

I try to detect rather the page status is 404 or not by this code :var request = new XMLHttpRequest();

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 an onreadstatechanged 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
Add a ment  | 

2 Answers 2

Reset to default 2

You'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

  1. 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.
  2. You need to add a callback for when the requests' state has changed. This is done with an onreadystatechange property off of your request 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信