Jquery Ajax parses simple examples of synchronous and asynchronous calls to XML data of

  • 2020-03-30 01:39:51
  • OfStack


$.ajax({
                async: true, //Default true(asynchronous request)
                cache: true, //By default, true, set to false, will not load the request information from the browser cache.
                type: "POST", //Default :GET request method :[POST/GET]
                dataType: "xml", //The default "XML"/" HTML "return data type: [" XML"/" HTML "/" script "/" json "/" json "]
                url: "Test.ashx", //Default current address, the address to which the request is sent
                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 successful request: server returns data, data format.
                    $("#users").empty();
                    //Dealing with XML data with Jquery
                    $(xml).find('Table').each(function() {
                        var loginname = $(this).find("Loginname").text();
                        var Name").text();
                        $("#users").append("<li>" + loginname + " - " + name + "</li>");
                    });
                    /*
                    $(xml).find('user').each(function(i) {
                        var loginname = $(xml).find("user loginname").eq(i).text();
                        var user name").eq(i).text();
                        $("#users").append("<p>" + loginname + "</p>" + "<p>" + name + "</p><Br />");
                    }) 
                    $(xml).find("student").each(function(i){
                        var id"); //Take the object
                        var id_value=$(this).children("id").text(); //Take the text
                        alert(id_value);//So this is the value of the ID.
                        alert($(this).attr("email")); //This is where the email property under student is displayed.
                        //And then finally, this is cssrain, which is a little bit more JQ than macnie
                        $('<li></li>').html(id_value).appendTo('ol');
                    });
                    */
                }
            })

Return XML data with ashx file:

<%@ 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("<?xml version="1.0" encoding="gbk"?>n " + ds.GetXml());
        /*
        StringBuilder sb = new StringBuilder();
        sb.Append("<?xml version="1.0" encoding="gbk"?>");
        sb.Append("<AccountList>");
        sb.Append("<Account><loginname>Loro5</loginname><name>wulu</name></user>");
        sb.Append("</Account>");
        context.Response.Write(sb.ToString());
        */

        context.Response.End();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}


Related articles: