[ASP.NET]JQuery AJAX用法整理

摘要:[ASP.NET]JQuery AJAX用法整理


我们再用Jquery CallBack Server时有许多参数要如何使用

$.ajax({
                type: "POST",
                url: "MyWebService.asmx/SayHelloJson",
                data: "{ firstName: 'Aidy', lastName: 'F' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var myData = JSON.parse(data.d);
}
$.ajax({
                type: "POST",
                url: "MyWebService.asmx/SayHello",
                data: "firstName=Aidy&lastName=F", /
                dataType: "text", 
                success: function (data) {
                    $("#searchresultsA").html(data); /
                }
            });

Type :postback 给server, Server会Respon 给Client

Url: “MyWebService.asmx/SayHelloJson?firstName=’aidy’&lastName=’F’”要注明方法 (Web Service)

传入参数,Server端可以用this.comtext.request.quertstring[“”] ,去取得get数据

“MyWebService.ahsx”(泛型处理常式)

“MyWebService.apsx”(一般网页)

Data: "{ firstName: 'Aidy', lastName: 'F' }" 可用JSON格式传入参数或是"firstName=Aidy&lastName=F"

Server端用this.comtext.request.From[“”],去取得post数据,

或是Web Server端会用Fuction(string firstName, string lastName)去取得post资

contentType: "application/json; charset=utf-8"如果是传送data JSON 格式给Server要特别注明

dataType : "json","text",”xml”,Server回传的数据型态

success : function (data){} :回传的数据跟动作

Web Server Code

[WebMethod]
public string SayHello(string firstName, string lastName)
{
    return "Hello " + firstName + " " + lastName;
}
[WebMethod]
public string SayHelloJson(string firstName, string lastName)
{
    var data = new { Greeting = "Hello", Name = firstName + " " + lastName };

    // We are using an anonymous object above, but we could use a typed one too (SayHello class is defined below)
    // SayHello data = new SayHello { Greeting = "Hello", Name = firstName + " " + lastName };

    System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

    return js.Serialize(data);
}

原文:大专栏  [ASP.NET]JQuery AJAX用法整理


猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11458410.html
今日推荐