SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 180 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

Beyond that, though, you should
add an extra level of error checking.
If you are expecting your data back in a particular format, such as XML or JSON, include
a server-based contingency plan that populates the return with an error code of some kind.
Then check for the error code on the client side before processing your results. If the server
doesn??™t return what you want (for example, it returns an invalid JSON object or an unhandled
server-side error), you??™ll need to handle that on the client side as well.
Here is a JSON example:
{"error":{"id":1,"message":"Your session has expired"}}
In the onsuccess event handler that you attached, you would have the following code:
var UNKNOWN = 0;
function processRequestSuccess(transport)
{
var obj = transport.responseText.parseJSON();
// if JSON parsing didn't work then no object exists
// which means the server failed somehow
if(!obj)
{
processError(UNKNOWN);
return;
}
// if I have an error property in my object, the server
// returned an error message and failed gracefully.
if(obj.error)
{
processError(obj.error.id, obj.error.message);
}
CHAPTER 5 n AJAX AND DATA EXCHANGE 123
// continue to process request normally
// ...
}
The processError() function would simply take the parameters and process the error.
It could display an alert dialog or it could write the error message to the page.
Using Libraries to Handle Ajax Calls
As you??™ve seen, there is a lot to consider each time an Ajax call is made.


Pages:
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
włosy naturalne adwokat prawo rodzinne Na czym polega pozycjonowanie tani hosting gier lampy żyrandole