admin管理员组文章数量:1022935
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
2 Answers
Reset to default 3This 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
}
}
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
2 Answers
Reset to default 3This 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
}
}
本文标签: javascriptXMLHttpRequest Open and Send How to tell if it workedStack Overflow
版权声明:本文标题:javascript - XMLHttpRequest Open and Send: How to tell if it worked - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745594470a2158081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论