[Ajax]JQuery Ajax用戶端,Web Sever asmx Server端, 參數傳遞

摘要:[Ajax]JQuery Ajax實做,接收JSON資料

前端網頁程式非同步功能Ajax 使用JQuery Ajax功能,取得資料後呈現資料

Server端可以使用Web Service .asmx 

 [WebMethod(enableSession: true)]
    public void HelloWorld()
    {
        //設定輸出格式為json格式
        this.Context.Response.ContentType = "application/json";
        //get
        String data1 = this.Context.Request.QueryString["val"];
        //post
        String data = this.Context.Request.Form["ret"];
        Dictionary dic = new Dictionary();
        dic["data"] = "Post取得的值為:"+data;
        dic["data1"] = "Get取得的值為:"+data1;

        //新版的可以用
        //System.Runtime.Serialization.Json.DataContractJsonSerializer
        JavaScriptSerializer serializer = new JavaScriptSerializer();    
        //輸出json格式
        this.Context.Response.Write(serializer.Serialize(dic));
    }

   [WebMethod(enableSession: true)]
    public void getName()
    {
    this.Context.Response.ContentType = "application/json";

    //get
    string a1 = Context.Request.QueryString["ID1"];
    //post
    string a2 = Context.Request.Form["ID2"];

        var responseEntities = new List()
        {
        new Person{ Name="Joey1", Id=a1},
        new Person{ Name="Joey2", Id=a2}
        };

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    //輸出json格式
    this.Context.Response.Write(serializer.Serialize(responseEntities));
        
    }