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 } |