JQuery呼叫asp.net的後臺方法

ForTechnology發表於2013-03-22
利用JQuery的$.ajax()可以很方便的呼叫asp.net的後臺方法。
[WebMethod]   名稱空間

1、無引數的方法呼叫, 注意:1.方法一定要靜態方法,而且要有[WebMethod]的宣告
後臺:

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


前臺:

$(function() {  
    $("#btnOK").click(function() {  
        $.ajax({  
            //要用post方式  
            type: "Post",  
            //方法所在頁面和方法名  
            url: "data.aspx/SayHello",  
            contentType: "application/json; charset=utf-8",  
            dataType: "json",  
            success: function(data) {  
                //返回的資料用data.d獲取內容  
                alert(data.d);  
            },  
            error: function(err) {  
                alert(err);  
            }  
        });  
 
        //禁用按鈕的提交  
        return false;  
    });  
});


2、帶引數的方法呼叫

後臺:

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


前臺:

$(function() {  
    $("#btnOK").click(function() {  
        $.ajax({  
            type: "Post",  
            url: "data.aspx/GetStr",  
            //方法傳參的寫法一定要對,str為形參的名字,str2為第二個形參的名字  
            data: "{'str':'我是','str2':'XXX'}",  
            contentType: "application/json; charset=utf-8",  
            dataType: "json",  
            success: function(data) {  
                //返回的資料用data.d獲取內容  
                  alert(data.d);  
            },  
            error: function(err) {  
                alert(err);  
            }  
        });  
 
        //禁用按鈕的提交  
        return false;  
    });  
});


3、返回陣列方法的呼叫

後臺:

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


前臺:

$(function() {  
    $("#btnOK").click(function() {  
        $.ajax({  
            type: "Post",  
            url: "data.aspx/GetArray",  
            contentType: "application/json; charset=utf-8",  
            dataType: "json",  
            success: function(data) {  
                //插入前先清空ul  
                $("#list").html("");  
 
                //遞迴獲取資料  
                $(data.d).each(function() {  
                    //插入結果到li裡面  
                    $("#list").append("
  • " + this + "
  • ");  
                    });  
     
                    alert(data.d);  
                },  
                error: function(err) {  
                    alert(err);  
                }  
            });  
     
            //禁用按鈕的提交  
            return false;  
        });  
    });
    ///
    $(function() {
        $("#btnOK").click(function() {
            $.ajax({
                type: "Post",
                url: "data.aspx/GetArray",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    //插入前先清空ul
                    $("#list").html("");
     
                    //遞迴獲取資料
                    $(data.d).each(function() {
                        //插入結果到li裡面
                        $("#list").append("
  • " + this + "
  • ");
                    });
     
                    alert(data.d);
                },
                error: function(err) {
                    alert(err);
                }
            });
     
            //禁用按鈕的提交
            return false;
        });
    });
     

    來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25897606/viewspace-756791/,如需轉載,請註明出處,否則將追究法律責任。

    相關文章