Ajax, how to get notified upon an error?

L

laredotornado

Hello,

I am misunderstanding the concept of the xmlHttpRequest.readyState
attribute. I would like to be notified when such a page is
successfully visited but have a different function invoked when an
error (e.g. 404) occurs. How would I do this? Here is the code I have
now ...

var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new
ActiveXObject("Microsoft.XMLHTTP");
}
var strURL = "myURL.html";
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
alert("Web page visited
successfully!");
}
}
self.xmlHttpReq.send("");

Thanks for any help, - Dave
 
S

scriptguru

(e-mail address removed) напиÑав:
Hello,

I am misunderstanding the concept of the xmlHttpRequest.readyState
attribute. I would like to be notified when such a page is
successfully visited but have a different function invoked when an
error (e.g. 404) occurs. How would I do this? Here is the code I have
now ...

var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new
ActiveXObject("Microsoft.XMLHTTP");
}
var strURL = "myURL.html";
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
alert("Web page visited
successfully!");
}
}
self.xmlHttpReq.send("");

Thanks for any help, - Dave

Have you tried to analyse server response?

Val
 
T

Tom Cole

Hello,

I am misunderstanding the concept of the xmlHttpRequest.readyState
attribute. I would like to be notified when such a page is
successfully visited but have a different function invoked when an
error (e.g. 404) occurs. How would I do this? Here is the code I have
now ...

var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new
ActiveXObject("Microsoft.XMLHTTP");
}
var strURL = "myURL.html";
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
alert("Web page visited
successfully!");
}
}
self.xmlHttpReq.send("");

Thanks for any help, - Dave

Try reading the XmlHttpRequest.status value. It should contain the
server response code.
 
T

Tom Cole

Tom said:
Try reading the XmlHttpRequest.status value. It should contain the
server response code.

BTW, this should be done once determining that readyState is 4. So for
example:

if (self.xmlHttpReq.readyState == 4) {
if (self.xmlHttpReq.status == 200) {
alert('All systems are go!');
}
else if (self.xmlHttpReq.status == 404) {
alert('Unable to locate resource');
}
else if (self.xmlHttpReq.status == 500) {
alert('There was an error.');
}
else {
alert('An unexpected error ' + self.xmlHttpReq.status + ' was
returned.');
}
}

You could easily replace the alerts with the desired method calls.
 
B

Bruce Wisentaner

Hello,

I am misunderstanding the concept of the xmlHttpRequest.readyState
attribute. I would like to be notified when such a page is
successfully visited but have a different function invoked when an
error (e.g. 404) occurs. How would I do this? Here is the code I have
now ...

var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new
ActiveXObject("Microsoft.XMLHTTP");
}
var strURL = "myURL.html";
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
alert("Web page visited
successfully!");
}
}
self.xmlHttpReq.send("");

Thanks for any help, - Dave

Dave:
Just look at the status member of the XmlHTTPRequest object; this will be an
RFC2616 HTTP status code (like 404 or whatever)
In your case (keeping it simple for example),
if (self.xmlHttpReq.readyState == 4)
{
if (self.xmlHttpReq.status == 200) /* HTTP "OK" */
alert("Web page visited successfully!");
else if (self.xmlHttpReq.status == 404)
alert("Something bad happened, cannot fetch page.");
else
alert("Something REALLY BAD happened, fire up the debugger!");
} /* readyState == 4 */

On the server side, you can put a text message with the HTTP errror code,
that can be accessed on client side with statusText member. Something like:
alert ("Something stinky happened: " + self.xmlHttpReq.statusText);

---Bruce Wisentaner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top