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

javascript - XMLHttpRequest Open and Send: How to tell if it worked - Stack Overflow

programmeradmin2浏览0评论

As is in the title, my question is, Is is possible to tell if the open and send methods from XMLhttpRequest actually worked? Is there any indicator? example code:

cli = new XMLHttpRequest();
cli.open('GET', '');
cli.send();

I'm trying to code in fault handling to this, but I need to be able to tell if the request failed so I can handle it.

As is in the title, my question is, Is is possible to tell if the open and send methods from XMLhttpRequest actually worked? Is there any indicator? example code:

cli = new XMLHttpRequest();
cli.open('GET', 'http://example/products');
cli.send();

I'm trying to code in fault handling to this, but I need to be able to tell if the request failed so I can handle it.

Share Improve this question asked Jul 5, 2012 at 20:03 BggreenBggreen 1231 gold badge4 silver badges10 bronze badges 4
  • 2 Yes. It is. What online resources/documentation that explain how to use XHR have been consulted? -1; read a few, then, if there are remaining unclear points, ask a more directed question. (I would remend using an XHR wrapper, but it's the same idea.) – user166390 Commented Jul 5, 2012 at 20:05
  • @pst It seems to me that the asynchronous nature of the operation may legitimately be hard to understand by a newer who thus could be blocked. That's the reason why I answered. Do you think I shouldn't have done it ? – Denys Séguret Commented Jul 5, 2012 at 20:22
  • @dystroy Except for the fact this is a well covered use case .. people wrote documentation/tutorials for a reason. – user166390 Commented Jul 5, 2012 at 20:25
  • Sorry I was just learning from w3schools./dom/dom_http.asp and i didnt realize that the ready state was in the hundreds and that it was updated dynamically. – Bggreen Commented Jul 5, 2012 at 20:35
Add a ment  | 

2 Answers 2

Reset to default 3

This is an an asynchronous operation. Your script continues its execution while the request is being sent.

You detect the state changes using a callback :

var cli = new XMLHttpRequest();
cli.onreadystatechange = function() {
        if (cli.readyState === 4) {
            if (cli.status === 200) {
                       // OK
                       alert('response:'+cli.responseText);
                       // here you can use the result (cli.responseText)
            } else {
                       // not OK
                       alert('failure!');
            }
        }
};
cli.open('GET', 'http://example/products');
cli.send();
// note that you can't use the result just here due to the asynchronous nature of the request
req = new XMLHttpRequest;
req.onreadystatechange = dataLoaded;
req.open("GET","newJson2.json",true);
req.send();

function dataLoaded()
{
    if(this.readyState==4 && this.status==200)
    {
        // success
    }
    else
    {
        // io error
    }
}
发布评论

评论列表(0)

  1. 暂无评论