Examples of Ajax and $. ajax usage (recommended), ajax. ajax

Source: Internet
Author: User

Examples of Ajax and $. ajax usage (recommended), ajax. ajax

Instance 1 (Basic Ajax request creation format ):

<Html xmlns = "http://www.w3.org/1999/xhtml"> 
<Head runat = "server"> <title> AjaxDemo instance </title> <script src = "JS/jQuery-1.4.1-vsdoc.js" type = "text/javascript"> </script> <script type = "text/javascript"> // use Ajax to read the work content of the browser function readRequest () {// ignore browser compatibility issues var xmlhttp = new XMLHttpRequest (); // open a server-related link // send a request // The Request Method (get/send), the request page, and whether the xmlhttp request is asynchronous. open ("GET", "AjaxDemo. aspx ", true); // send data xmlhttp. send (null); // receive the result xmlhttp returned by the server. onreadystatecha Nge = function () {// request to complete if (xmlhttp. readyState = 4) {// if (xmlhttp. status = 200) {// output the content of the browser var result = xmlhttp. responseText; alert (result); window. alert ("the browser content is read successfully! ") ;}};}; Function btn_Click () {var http = new ActiveXObject (" Microsoft. XMLHTTP "); // or use this sentence to create var xmlhttp = new XMLHttpRequest (); if (! Http) {alert ("An error occurred while creating the xmlhttp object! "); Return false;} http. open ("POST", "AjaxDemo. ashx ", false); http. onreadystatechange = function () {if (http. readyState = 4) {// if (http. status = 200) {alert (http. responseText); document. getElementById ("Text1 "). value = http. responseText;} else {window. alert ("error returned by Ajax server! ") ;}}; Http. send ();}; </script> 

Instance 2 (see attachment)

Consider browser compatibility Ajax request processing to obtain the background xml file content.

Instance 3 (see attachment)

Use $. Ajax to obtain the content of the xml file read from the background.

Function readXML1 () {// create an XML Object var xmldom = new ActiveXObject ("Microsoft. XMLDOM "); // set it to asynchronous xmldom. async = "false"; // load the XML document xmldom to be read. load ("XML1.xml"); info = ""; // The root node var node to be read = xmldom. selectNodes ("student"); // read the content in sequence. info = node [0]. childNodes [0]. nodeTypedValue + "<br/>" + node [0]. childNodes [1]. nodeTypedValue + "<br/>" + node [0]. childNodes [2]. nodeTypedValue; document. getElementById ("xmlmsg "). innerHTML = info ;};
Function readXML2 () {// instantiate the xml Object var xml = new ActiveXObject ("Microsoft. XMLDOM "); // sets xml asynchronously. async = "false"; // load the XML file to be read. load ("XML2.xml"); info = ""; // select the name of the object to be read var fnode = xml.doc umentElement. selectNodes ("people"); // the content of the circular output document for (var I = 0; I <fnode. length; I ++) {for (var j = 0; j <fnode [I]. childNodes. length; j ++) {info + = fnode [I]. childNodes [j]. text + "<br/>" ;}} document. getElementById ("xmlmsg "). innerHTML = info ;};

Well, the above is the Ajax and $. I hope it will be helpful for you to explain how to use ajax instances. If you have any questions, please leave a message and I will reply to you in a timely manner!

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.