javascript - Ajax onerror handler not working -
in pure javascript: make ajax request.
the server supposed return 410 error.
my code:
var ajax = new xmlhttprequest(); ajax.onload = displayxml; ajax.onerror = displayerror; ajax.open('get', 'http://myurl.com/request?param=1¶m=2', true); ajax.send(); function displayerror(e) { console.log(this.status); } function displayxml() { console.log(this.responsexml); }
problem is, onerror
function never gets called when http 410 returned. console says "get .... 410 (gone)" on line ajax.send()
appears, calls displaypopularity
. how should handle http errors, if can't use .onerror
? mean, roll error handling displaypopularity
thought there more elegant solution.
i believe onerror security issues (eg. cross-origin) or errors in "config" ajax (front-end).
"http errors" not considered "errors", "responses" server.
xmlhttprequest.onload
executed, if there error in "http response":
try onreadystatechange
if
:
function displayxml() { if (this.readystate == 4) //this line equivalent ajax.onload { if (this.status == 200) //detect http status response (200 = ok) { console.log(this.responsexml); } else {//if 200 <> status = http error console.log(this.status); } } } var ajax = new xmlhttprequest(); //create obj ajax.open('get', 'http://myurl.com/request?param=1¶m=2', true); //config request ajax.onreadystatechange = displayxml; //add event ajax.send(null); //send request
Comments
Post a Comment