User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 330,219 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,913 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1180 | Replies: 7
Reply
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

ajax server-returned message?

  #1  
Nov 27th, 2007
hi,

i understand that the xmlHttp.responseText; attribute would return the server's message, but is there any way to get only ajax object's error messages?

see, i have a case.

lets say i have two different span on my page.

<span id="system"/>
<span id="display"/>

if ajax's object is throwing an error, i want it displayed on 'system' span. Otherwise if everything's normal, a tabled result will show in 'display' span. Is this possible?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: ajax server-returned message?

  #2  
Nov 27th, 2007
If by 'error message' you mean an error in the server side code; the error message is returned in the response text. If the error message is generated by conventional means; the xmlhttp object's 'status' property will be anything but 200, where 'anything but' is one of the standard HTTP status codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html The 400+ range off codes are usually errors.

If you mean an error in the client-side code; use some try/catch blocks or pre-checks to find the error and output the message.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: ajax server-returned message?

  #3  
Nov 27th, 2007
hmm, what i meant were the exception errors thrown from server side (in my case, php).

let's say after i do an insert, an exception was thrown from the server, i need to be able to identify whether an exception was thrown on the client side's ajax response.

i'm thinking of sending a true boolean on each successful transaction if ajax's object can't automatically group exceptions on a...say, responseError instead of mixing errors and non-errors.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: ajax server-returned message?

  #4  
Nov 27th, 2007
An uncaught exception server side should cause the response to be an 'error 500 internal server error'. I think PHP generates its own descriptive error pages; but it still may respond with a status other than 200.. If you didn't know; every response from a server starts with a 'header' which has important information; a status code, the page's content-type, user-provided data, and a few other things. We don't see that in view-source, and PHP doesn't force you to consider it, but it's there. Use this http://web-sniffer.net/ to view the header of a page that raises an error. See if the HTTP status code is something other than "HTTP/1.1 200 OK". The property ajaxobject.status returns the number of the status code: in this case 200. You can't split up the response from a server into 'errors' or 'non-errors' unless you define how to split it up. There is no such thing as an 'error' in a valid response : it is all basically plain text. The header tells you what the response should be treated as. [ and you can, via PHP, set the response to have any header/status you desire ]
Last edited by MattEvans : Nov 27th, 2007 at 4:34 am.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: ajax server-returned message?

  #5  
Nov 27th, 2007
hi, im not entirely fluent in the http headers, but i just did a test,

with an ajax page that calls php to save something. I then tried throwing an exception from php, but ajax's status is still 200.

So does this mean i have to return other http status code manually incase i catch an exception?
Reply With Quote  
Join Date: Sep 2005
Posts: 557
Reputation: digital-ether will become famous soon enough digital-ether will become famous soon enough 
Rep Power: 5
Solved Threads: 33
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Posting Pro

Help Re: ajax server-returned message?

  #6  
Nov 28th, 2007
Originally Posted by adrive View Post
hi, im not entirely fluent in the http headers, but i just did a test,

with an ajax page that calls php to save something. I then tried throwing an exception from php, but ajax's status is still 200.

So does this mean i have to return other http status code manually incase i catch an exception?


Do you mean exceptions like: a try, catch block and throw in PHP5?

XHR object reflects the HTTP Request and Response in the HTTP protocol. Since PHP5 exceptions are only on the PHP code level, they are not seen as an exception by XHR.
Only HTTP Response Codes are reflected in the XHR object.

What you can do is depending on the format of your HTTP Response, let the client know that an exception was encountered.
For this you need a more structured format than just plain text, maybe JSON or XML. XHR understands XML natively and thus will parse it out into a DOM Object that you can work with. JavaScript can parse JSON with a simple eval().

For example w/ XML:

<?php
// example exception handling for XHR in XML

// set content-type as xml
header('Content-Type: text/xml');


// PHP 5
try {
    doSomething();
} catch (Exception $e) {
    // handle either error here
    echo '<reponse>';
    echo '<exception msg="'.$e->getMessage().'" />';
    echo '</response>';
    exit();
}
?> 

For example w/ JSON:

<?php
// example exception handling for XHR in JSON format

// set content-type as javascript
header('Content-Type: text/javascript');


// PHP 5
try {
    doSomething();
} catch (Exception $e) {
    // handle either error here
    echo '{exception: {msg:\''.$e->getMessage().'\'}}';
    exit();
}
?> 

Then on your client side:

If you use XML, get the XHR.responseXML DOM object after the XHR response is received. Traverse the DOM tree for the exception node and get its message attribute value.
If you use JSON for example, just eval("(".XHR.responsetext.")"); saving it to a variable and get the exception property.

You could also have PHP modify the HTTP Response to reflect an exception occurring in the PHP code. Then you can test for exceptions in the XHR Object directly.

Note: The HTTP Response is just plain text. XHR will only parse out the HTTP headers and body into the XHR properties. It cannot understand an exception in any server side language. You have to code this into your Client.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: ajax server-returned message?

  #7  
Nov 28th, 2007
So does this mean i have to return other http status code manually incase i catch an exception?

That's what I would do; if PHP doesn't abort with true HTTP errors, then catch every possible error at the PHP level, set a 4xx or 5xx error status code and respond the error message, otherwise set a 200 status ( its the default so you dont have to do anything ) and respond a 'success' message or whatever else it is that you want to respond with on success.

I forgot to mention, if using headers manually; you must set the status and header before responding any other content - this might mean you have to buffer output until you're sure that the response is error free, or decide very quickly whether the response is an error or not. Also, you'd have to mark en entire response as either an error or a success, so, you wouldn't be able to mix error messages with success messages.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: ajax server-returned message?

  #8  
Nov 28th, 2007
thanks guys
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Marketplace (Sponsored Links)
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 3:48 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC