Discussion on error handling mechanism of Ajax

Source: Internet
Author: User
Tags object error handling return window client
Ajax| Error | Error handling
What are the hottest words in the current web technology? is Ajax. The core of the AJAX framework component is the XMLHttpRequest JavaScript object, which allows the client developer to send and receive XML documents over HTTP without disrupting user action and without exploiting hidden pages.
Now, some people may feel fear because it suddenly allows client developers who are likely to use validated forms and animated images too much to pass XML documents and process HTTP header information, but without risk there is no gain. We don't have to be afraid, I'll demonstrate how to use XMLHttpRequest to add some previously impossible, unworkable features that also reduce errors and improve product quality.



   XMLHttpRequest and XML DOM in JavaScript

First, we need to establish some rules. Special XMLHttpRequest Objects and general XML DOM are widely supported by the latest browsers (IE, Mozilla, Safari, Opera), although in general, Microsoft has a slight increase in its implementation and requires some special processing. Even though more of our friends have directly implemented XMLHttpRequest, IE asks you to instantiate a activexobject with the same attributes. An overview and a list of all the features can be found on the Apple developer relationship site.

The following is a basic example:

var req;
function Postxml (xmldoc) {
if (window. XMLHttpRequest) req = new XMLHttpRequest ();
else if (window. ActiveXObject) req = new ActiveXObject ("Microsoft.XMLHTTP");
else return; Failed.
Req.open (method, Serveruri);
Req.setrequestheader (' Content-type ', ' text/xml ');
Req.onreadystatechange = xmlposted;
Req.send (xmldoc);
}
function xmlposted () {
if (req.readystate!= 4) return;
if (Req.status = = 200) {
var result = Req.responsexml;
} else {
Failed.
}
}

There are many potential users of this powerful feature, and the search for what it might do is just beginning. But before you try to build XML functionality on the Web, I suggest you set up a "safety net" to keep your ambitions (ideas) from being hit.

   JavaScript error Handling Basics

JavaScript has been there for a long time, and its earlier versions are primitive, lacking features, just implemented. The latest browsers not only support the Try/catch/finally keyword in C + + and Java, but also implement the OnError event, which can capture any errors that occur at run time. The use of it is very straightforward:

function riskybusiness () {
try {
RiskyOperation1 ();
RiskyOperation2 ();
catch (e) {
E is an error-type object with at least two properties: name and message
finally {
Purge messages
}
}
Window.onerror = HandleError; Catch all the wrong safety nets
function handleerror (message, URI, line) {
Prompts the user that this page may not respond correctly
return true; Stop the default message
}









Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.