Discussion on the error handling mechanism of AJAX (1)

Source: Internet
Author: User
Tags add object error handling implement net return window client
ajax | error | error handling The core of the AJAX framework component is the XMLHttpRequest JavaScript object, which allows client developers to send and receive XML documents via HTTP without interrupting user operations and using hidden pages.

<table border = "0" cellspacing = "0" cellpadding = "0" align = "left" style = "margin-top: 10px; margin-right: 7px; margin-bottom: 3px; margin-left: 0px"> <tr> <td> <span id = "ad_pcdog_big"> </ span> </ td> </ tr> </ table>

Now, some people may be scared, because it suddenly allows client developers who may be using too many validation forms and animated images to be responsible for passing XML documents and processing HTTP headers, but there is no benefit without risk. Let's not be afraid, I will demonstrate how to use XMLHttpRequest to add some previously impossible and unfeasible features. It also reduces errors and improves product quality.

</ p> <p> <STRONG> XMLHttpRequest and XML DOM in JavaScript </ STRONG> </ p> <p>

First, we need to establish some rules. The special XMLHttpRequest object and the general XML DOM are widely supported by the latest browsers (IE, Mozilla, Safari, Opera), although in general, Microsoft will slightly add something to its implementation, requiring some special Processing. Although more of our friends directly implement XMLHttpRequest, IE still requires you to instantiate an ActiveXObject with the same properties. A related overview and a list of all features can be found on the Apple Developer Relations site.

Here is a basic example: </ p> <TABLE borderColor = # ffdddd width = "90%" align = center bgColor = # e3f3f3 border = 1> <TR> <TD> var req; <br /> <br / > function postXML (xmlDoc) {<br /> <br  if (window.XMLHttpRequest) req = new XMLHttpRequest();<br /> <br /> else if (window.ActiveXObject) req = new ActiveXObject ("Microsoft .XMLHTTP "); <br /> <br /> else return; // failed <br /> <br /> req.open (method, serverURI); <br /> <br /> req.setRequestHeader (' content-type ',' text / xml '); <br /> <br /> req.onreadystatechange = xmlPosted; <br /> <br /> req.send (xmlDoc); <br /> <br />} <br /> <br /> function xmlPosted () {<br /> <br  if (req.readyState != 4) return;<br /> <br /> if (req.status == 200) { <br /> <br /> var result = req.responseXML; <br /> <br />} else {<br /> <br /> // failed <br /> <br />} <br / > <br />} </ TD> </ TR> </ TABLE> <br /> <br />

There are many potential users of this powerful function, and the exploration of its possible functions has just begun. But before you try to build XML functionality on the web, I suggest you set up a "safety net" to ensure that your ambitions (ideas) will not be hit. <br /> <br /> <STRONG> JavaScript error handling basics </ STRONG> <br /> <br /> JavaScript has been around for a long time, and its early versions are relatively primitive, lacking features, and just implemented. The latest browsers not only support the try / catch / finally keywords in C ++ and Java, but also implement the onerror event, which can catch any errors that occur during runtime. Its use is very straightforward: <br /> <br /> <TABLE borderColor = # ffdddd width = "90%" align = center bgColor = # e3f3f3 border = 1> <TR> <TD> function riskyBusiness () { <br /> <br /> try {<br /> <br /> riskyOperation1 (); <br /> <br /> riskyOperation2 (); <br /> <br />} catch (e) {<br /> <br /> // e is an Error type object with at least two attributes: name and message <br /> <br /> finally {<br /> <br /> // clear message <br /> /> <br  }<br /> <br />} <br /> <br /> window.onerror = handleError; // safety net to catch all errors <br /> <br /> function handleError (message , URI, line) {<br /> <br /> // Prompt the user that this page may not respond properly <br /> <br /> return true; // Stop the default message <br /> <br />} </ TD> </ TR> </ TABLE>

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.