By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

Ajax, how to get notified upon an error?

P: n/a
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

Sep 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

la***********@zipmail.com написав:
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

Sep 18 '06 #2

P: n/a

la***********@zipmail.com wrote:
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.

Sep 18 '06 #3

P: n/a

Tom Cole wrote:
la***********@zipmail.com wrote:
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.
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.

Sep 19 '06 #4

P: n/a
<la***********@zipmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
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
Sep 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.