Jquery. Ajax of method call Asp.Net background method parsing

  • 2020-03-30 01:42:46
  • OfStack

JQuery's $.ajax() makes it easy to call asp.net's backend methods.
Let's warm up with a simple example.

1. Method call without parameters
Asp.net code:


using System.Web.Script.Services;   

[WebMethod]   
public static string SayHello()   
{   
     return "Hello Ajax!";   
}  
using System.Web.Script.Services;
[WebMethod]
public static string SayHello()
{
     return "Hello Ajax!";
} 

Note: 1. Methods must be static and have a WebMethod declaration

JQuery code:


/// <reference path="jquery-1.4.2-vsdoc.js"/>   
$(function() {   
    $("#btnOK").click(function() {   
        $.ajax({   
            //You want to use post & cake;  
            type: "Post",   
            //Method page and method name & NBSP;  
            url: "data.aspx/SayHello",   
            contentType: "application/json; charset=utf-8",   
            dataType: "json",   
            success: function(data) {   
                //The returned data is retrieved with data.d  
                alert(data.d);   
            },   
            error: function(err) {   
                alert(err);   
            }   
        });   

        //Disable button submission & NBSP;  
        return false;   
    });   
});  
/// <reference path="jquery-1.4.2-vsdoc.js"/>
$(function() {
    $("#btnOK").click(function() {
        $.ajax({
            //I'm going to post it
            type: "Post",
            //Method page and method name
            url: "data.aspx/SayHello",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                //The returned data is retrieved with data.d
                alert(data.d);
            },
            error: function(err) {
                alert(err);
            }
        });
        //Disable button submission
        return false;
    });
}); 

Results:

2. Method call with parameters
Asp.net code:


using System.Web.Script.Services;   

[WebMethod]   
public static string GetStr(string str, string str2)   
{   
    return str + str2;   
}  
using System.Web.Script.Services;
[WebMethod]
public static string GetStr(string str, string str2)
{
    return str + str2;
} 

JQuery code:

/// <reference path="jquery-1.4.2-vsdoc.js"/>   
$(function() {   
    $("#btnOK").click(function() {   
        $.ajax({   
            type: "Post",   
            url: "data.aspx/GetStr",   
            //Method must be written correctly, STR is the name of the parameter,str2 is the name of the second parameter & NBSP;  
            data: "{'str':' I am a ','str2':'XXX'}",   
            contentType: "application/json; charset=utf-8",   
            dataType: "json",   
            success: function(data) {   
                //The returned data is retrieved with data.d  
                  alert(data.d);   
            },   
            error: function(err) {   
                alert(err);   
            }   
        });   

        //Disable button submission & NBSP;  
        return false;   
    });   
});  
/// <reference path="jquery-1.4.2-vsdoc.js"/>
$(function() {
    $("#btnOK").click(function() {
        $.ajax({
            type: "Post",
            url: "data.aspx/GetStr",
            //Method to pass the parameter must be written correctly, STR is the name of the parameter,str2 is the name of the second parameter
            data: "{'str':' I am a ','str2':'XXX'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                //The returned data is retrieved with data.d
                  alert(data.d);
            },
            error: function(err) {
                alert(err);
            }
        });
        //Disable button submission
        return false;
    });
}); 

Operation results:

Now let's go to the advanced application

Returns the call to the array method
Asp.net code:


using System.Web.Script.Services;   

[WebMethod]   
public static List<string> GetArray()   
{   
    List<string> li = new List<string>();   

    for (int i = 0; i < 10; i++)   
        li.Add(i + "");   

    return li;   
}  
using System.Web.Script.Services;
[WebMethod]
public static List<string> GetArray()
{
    List<string> li = new List<string>();
    for (int i = 0; i < 10; i++)
        li.Add(i + "");
    return li;
} 

JQuery code:

/// <reference path="jquery-1.4.2-vsdoc.js"/>   
$(function() {   
    $("#btnOK").click(function() {   
        $.ajax({   
            type: "Post",   
            url: "data.aspx/GetArray",   
            contentType: "application/json; charset=utf-8",   
            dataType: "json",   
            success: function(data) {   
                //Empty ul  before inserting;  
                $("#list").html("");   

                //Recursively getting data & NBSP;  
                $(data.d).each(function() {   
                    //Insert the result into li & NBSP;  
                    $("#list").append("<li>" + this + "</li>");   
                });   

                alert(data.d);   
            },   
            error: function(err) {   
                alert(err);   
            }   
        });   

        //Disable button submission & NBSP;  
        return false;   
    });   
});  
/// <reference path="jquery-1.4.2-vsdoc.js"/>
$(function() {
    $("#btnOK").click(function() {
        $.ajax({
            type: "Post",
            url: "data.aspx/GetArray",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                //Empty ul before inserting
                $("#list").html("");
                //Get the data recursively
                $(data.d).each(function() {
                    //Insert the result into li
                    $("#list").append("<li>" + this + "</li>");
                });
                alert(data.d);
            },
            error: function(err) {
                alert(err);
            }
        });
        //Disable button submission
        return false;
    });
}); 

Operation results:

Returns a call to the Hashtable method
Asp.net code:


using System.Web.Script.Services;   
using System.Collections;   

[WebMethod]   
public static Hashtable GetHash(string key,string value)   
{   
    Hashtable hs = new Hashtable();   

    hs.Add("www", "yahooooooo");   
    hs.Add(key, value);   

    return hs;   
}  
using System.Web.Script.Services;
using System.Collections;
[WebMethod]
public static Hashtable GetHash(string key,string value)
{
    Hashtable hs = new Hashtable();
    hs.Add("www", "yahooooooo");
    hs.Add(key, value);

    return hs;
} 

JQuery code:

/// <reference path="jquery-1.4.2-vsdoc.js"/>   
$(function() {   
    $("#btnOK").click(function() {   
        $.ajax({   
            type: "Post",   
            url: "data.aspx/GetHash",   
            //Remember to put double quotation marks & NBSP; T_T    
            data: "{ 'key': 'haha', 'value': ' Ha ha! ' }",   
            contentType: "application/json; charset=utf-8",   
            dataType: "json",   
            success: function(data) {   
                alert("key: haha ==> "+data.d["haha"]+"n key: www ==> "+data.d["www"]);   
            },   
            error: function(err) {   
                alert(err + "err");   
            }   
        });   

        //Disable button submission & NBSP;  
        return false;   
    });   
});  
/// <reference path="jquery-1.4.2-vsdoc.js"/>
$(function() {
    $("#btnOK").click(function() {
        $.ajax({
            type: "Post",
            url: "data.aspx/GetHash",
            //Remember to put double quotation marks & NBSP; T_T
            data: "{ 'key': 'haha', 'value': ' Ha ha! ' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                alert("key: haha ==> "+data.d["haha"]+"n key: www ==> "+data.d["www"]);
            },
            error: function(err) {
                alert(err + "err");
            }
        });
        //Disable button submission
        return false;
    });
}); 

Operation results:

5. Manipulate XML
XMLtest. XML:


<?xml version="1.0" encoding="utf-8" ?>  
<data>  
  <item>  
    <id>1</id>  
    <name>qwe</name>  
  </item>  
  <item>  
    <id>2</id>  
    <name>asd</name>  
  </item>  
</data>  
<?xml version="1.0" encoding="utf-8" ?>
<data>
  <item>
    <id>1</id>
    <name>qwe</name>
  </item>
  <item>
    <id>2</id>
    <name>asd</name>
  </item>
</data>

JQuery code:

$(function() {   
    $("#btnOK").click(function() {   
        $.ajax({   
            url: "XMLtest.xml",   
            dataType: 'xml', //The return type is XML, not the same as the Json before & NBSP;  
            success: function(xml) {   
                //Empty list   
                $("#list").html("");   
                //Find the XML element & NBSP;   KVM online shopping brush website construction Beijing express company ultrasonic welding machine
                $(xml).find("data>item").each(function() {   
                    $("#list").append("<li>id:" + $(this).find("id").text() +"</li>");   
                    $("#list").append("<li>Name:"+ $(this).find("name").text() + "</li>");   
                })   
            },   
            error: function(result, status) { //If you don't have the above catch error you execute the callback function here & NBSP;  
                alert(status);   
            }   
        });   

        //Disable button submission & NBSP;  
        return false;   
    });   
});  
$(function() {
    $("#btnOK").click(function() {
        $.ajax({
            url: "XMLtest.xml",
            dataType: 'xml', //The return type is XML, which is different from Json
            success: function(xml) {
                //To empty the list
                $("#list").html("");
                //Finding XML elements
                $(xml).find("data>item").each(function() {
                    $("#list").append("<li>id:" + $(this).find("id").text() +"</li>");
                    $("#list").append("<li>Name:"+ $(this).find("name").text() + "</li>");
                })
            },
            error: function(result, status) { //If you don't have the catch above, you will execute the callback function here
                alert(status);
            }
        });
        //Disable button submission
        return false;
    });
}); 


Related articles: