Ajax
XMLHttpRequestis the object that enables the JavaScript code to make asynchronous HTTP server requests. This allows you to initiate HTTP requests and receive responses from the server in the background, without requiring the user to submit the page to the server. This feature, combined with the possibility to manipulate the web page using DOM and CSS, allows you to implement responsive functionality and visual effects backed with live data from the server, without the user experiencing any visual interruptions. This is AJAX.- From: http://www.cristiandarie.ro/asp-ajax/Async.html
- The typical sequence of operations when working with
XMLHttpRequestis as follows:- Create an instance of the
XMLHttpRequestobject. - Use the
XMLHttpRequestobject to make an asynchronous call to a server page, defining a callback function that will be executed automatically when the server response is received. - Read the server's response in the callback function.
- Update the web page using the data received from the server.
- Go to step 2
- Create an instance of the
- The following JavaScript function creates an
XMLHttpRequestinstance by using the native object if available, or theMicrosoft.XMLHttpActiveX control for visitors that use Internet Explorer 6 or older:// creates an XMLHttpRequest instance function createXMLHttpRequestObject() { // xmlHttp will store the reference to the XMLHttpRequest object var xmlHttp; // try to instantiate the native XMLHttpRequest object try { // create an XMLHttpRequest object xmlHttp = new XMLHttpRequest(); } catch(e) { // assume IE6 or older try { xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); } catch(e) { } } // return the created object or display an error message if (!xmlHttp) alert("Error creating the XMLHttpRequest object."); else return xmlHttp; }
- This function uses the JavaScript
try/catchconstruct, which is a powerful exception-handling technique that was initially implemented in OOP (Object Oriented Programming) languages. Basically, when an error happens at run time in the JavaScript code, an exception is thrown. The exception is an object that contains the details of the error. Using thetry/catchsyntax, you can catch the exception and handle it locally, so that the error won't be propagated to the user's browser. try/catch :try { // code that might generate an exception } catch (e) { // code that executes if an exception was thrown in the try block // (exception details are available through the e parameter) }
You place any code that might generate errors inside the
tryblock. If an error happens, the execution is passed immediately to thecatchblock. If no error happens inside thetryblock, then the code in thecatchblock never executes.Run-time exceptions propagate from the point they were raised, up through the call stack of your program. The call stack is the list of methods that are being executed. So if a functionA()calls a functionB()which at its turn calls a function calledC(), then the call stack will be formed of these three methods. If an exception happens inC(), you can handle it using atry/catchblock right there. If the exception isn't caught and handled inC(), it propagates, toB(), and so on. The final layer is the web browser. If your code generates an exception that you don't handle, the exception will end up getting caught by the web browser, which may display an unpleasant error message to your visitor. - Another technique is to use a JavaScript feature called object detection. This feature allows you to check whether a particular object is supported by the browser, and works like this:
if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); }
- n
- n
- n
- n
- n
- n
浙公网安备 33010602011771号