473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ajax, how to get notified upon an error?

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
4 4033

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

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

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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
6289
by: Steve | last post by:
I need to build a very dynamic client and would be interested in knowing the pros and cons of using JSF and Ajax to accomplish this. Thanks. Steve
17
13148
by: petermichaux | last post by:
Hi, Is it possible for an AJAX request to be left open for multiple responses? This could avoid repetitive polling of the server. Thanks, Peter
4
1573
by: Frances | last post by:
am just starting out with AJAX, got example from here, http://www.w3schools.com/ajax/ajax_example.asp working fine but I have a question (actually I have many questions, but will start with this...
4
4036
by: Frances | last post by:
I literally started learning AJAX just last weekend.. I have this page, http://www.francesdelrio.com/ajax/db2.html, where I'm essentially doing what's here,...
31
3075
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
5
2199
by: Martin | last post by:
Hello NG, I've been doing some AJAX for a few weeks now. The basics worked fine so far, but now I've got the following problem which I can't solve: With AJAX you typically update/replace only...
2
3185
by: gnewsgroup | last post by:
I have a label control, which is wrapped up by <asp:UpdatePanel><ContentPlateetc. The Text property of the label is set upon a button click. This works fine. But if I navigate to another page...
0
2903
by: sanrek | last post by:
Hi Folks, Slider control is driving me nuts, below is the code from my test page's page load event, nothing great about code, I have a place holder control into which I am adding a table which...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.