This is basically an introductory tutorial for copying reco. In reco, the Ajax mechanism is encapsulated in an object-oriented manner, and two simple response modes are designed: Element update and JS response. Here we will only look at the element update. Writing an AJAX application from scratch is troublesome, but using a library written by others will get twice the result with half the effort. Reco only requires a trilogy: 1. register the Ajax request processor. It is the stuff that sends Ajax requests. The first line of code is as follows. Register the Response processor. In the element update mode, the processor is an element. Here, you only need to register the element ID. For example, the second line of code will automatically update the AJAX response to the element. function bodyOnLoad() { ajaxEngine.registerRequest("getxx", "hotman_x.xml"); //1 ajaxEngine.registerAjaxElement("xx"); // 2 } Generally, this function is activated as the onload event of the body. It can be seen from the name, huh, huh. 2. Write the HTML subject. Of course, you must have an HTML element with the ID xx and the content available for update. Elements such as <br> cannot be dropped. 3. Send an AJAX request: ajaxEngine. sendRequest ("getxx"); of course, to execute this statement, you need a method to call it, such as placing a button or something. The JS part is complete. The background also needs to do a little bit, that is, send an xml response to the corresponding call. Note the preceding statement 1. The second parameter is to request an XML document with response data. This is for the sake of simplicity. In actual situations, I am afraid this parameter will not be a static page. It is generally a dynamic page such as JSP and ASP. This dynamic page will provide an XML response. However, pay attention to a small problem, that is, the HTTP response Header must explicitly indicate that the MIME type of the returned document is XML, as shown in JSP: <% response. setHeader ("Content-Type", "text/xml"); %>. After talking about it for a long time, we should look at the Code: <pre> <script src="prototype.js"></script> <script src="rico.js"></script> <script> function bodyOnLoad() { ajaxEngine.registerRequest("getxx", "hotman_x_x.xml"); ajaxEngine.registerAjaxElement("xx"); } function requestIt() { ajaxEngine.sendRequest("getxx"); } </script>
<body onload="javascript:bodyOnLoad()"> <div id="xx"></div> <input type="button" onclick="javascript:requestIt()"/> </body>
Here is the response XML, which also has certain format requirements: <?xml version="1.0" encoding="ISO-8859-1"?> <ajax-response> <response type="element" id="xx"> <span>hello, world!</span> </response> </ajax-response> In fact, there are only three points to note: first, ajax-response is the root node, and second, all the lower-level subnodes of the root node are response, and its attribute type is set to element (that is, it is set to the element update mode). id should be the registration ID of the element to be updated. |