JqueryAjax parses XML data (synchronous and asynchronous calls) simple instance _ jquery

Source: Internet
Author: User
This article mainly introduces a simple example of JqueryAjax parsing XML data (synchronous and asynchronous calls). If you need some help, please refer to it. The Code is as follows:


$. Ajax ({
Async: true, // default value: true (asynchronous request)
Cache: true, // The default value is true. setting this parameter to false will not load request information from the browser cache.
Type: "POST", // default: GET Request Method: [POST/GET]
DataType: "xml", // default ["xml"/"html"] return data type: ["xml"/"html"/"script"/"json"/"jsonp"]
Url: "Test. ashx", // The default current address, the address that sends the request
Data: {key: "value"}, // data sent to the server
Error: function (xml) {alert ('error loading XML document' + xml) ;}, // called when the request fails
Timeout: 1000, // set the request timeout
Success: function (xml) {// callback function parameter after the request is successful: data returned by the server, data format.
$ ("# Users"). empty ();
// Use Jquery to process xml data
$ (Xml). find ('table'). each (function (){
Var loginname = $ (this). find ("Loginname"). text ();
Var Name "). text ();
$ ("# Users"). append ("

  • "+ Loginname +"-"+ name +"
  • ");
    });
    /*
    $ (Xml). find ('user'). each (function (I ){
    Var loginname = $ (xml). find ("user loginname"). eq (I). text ();
    Var user name "). eq (I). text ();
    $ ("# Users"). append ("

    "+ Loginname +"

    "+"

    "+ Name +"


    ");
    })
    $ (Xml). find ("student"). each (function (I ){
    Var id "); // retrieves an object
    Var id_value = $ (this). children ("id"). text (); // get text
    Alert (id_value); // here is the ID value.
    Alert ($ (this). attr ("email"); // The email attributes under student are displayed here.

    // The final output is shown. This is the CSS rain method, which seems to be a little more JQ than macnie.
    $ ('

  • '0000.html (id_value). appendTo ('ol ');
    });
    */
    }
    })


    Use the ashx file to return XML data:

    The Code is as follows:


    <% @ WebHandler Language = "C #" %>

    Using System;
    Using System. Web;
    Using System. Text;
    Using System. Data;

    Public class Test: IHttpHandler {

    Public void ProcessRequest (HttpContext context ){
    Context. Response. StatusCode = 200;
    Context. Response. Cache. SetCacheability (HttpCacheability. NoCache );

    DataSet ds = new DataSet ("AccountList ");
    Ds = GetList ("Account", "AccountId", "Loginname, Name", 50, 1, false, false, "1 = 1 ");
    Context. Response. ContentType = "text/xml ";
    Context. Response. Charset = "GB2312 ";
    Context. Response. Clear ();
    Context. Response. Write (" \ N "+ ds. GetXml ());

    /*
    StringBuilder sb = new StringBuilder ();
    Sb. Append (" ");
    Sb. Append ("");
    Sb. Append (" Loro5 Wulu ");
    Sb. Append ("");
    Context. Response. Write (sb. ToString ());
    */


    Context. Response. End ();

    }

    Public bool IsReusable {
    Get {
    Return false;
    }
    }

    }

    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.