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

As is in the title, my question is, Is is possible to tell if the open and send methods from XMLhttpReq

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
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745594472a4635026.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信