最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论