简单的json返回值asp.net与webservices

时间:2015-06-06 10:01:19

标签: jquery json asp.net-ajax asp.net-2.0

简单的json返回值asp.net与webservices我将显示我尝试的代码

当Button单击时,它会在json中返回一些值

Asp.net HTML编码

<input type="button" id="btnSubmit" value="Submit"  />

JQuery编码

$(document).ready(function(){
    $("#btnSubmit").click(function(){
        txtNameValue = $("#txtName").val();
       alert("button clicked");

        $.ajax({
        type: "POST",
        url: serviceURL+"/HelloWorld",
        data: "{'name':'" + txtNameValue + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        success:function(response) {
        alert(response);
            console.log(typeof response);

        }
        });

    });    
});

Web服务文件类文件

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class FirstPageWebService : System.Web.Services.WebService {

    public FirstPageWebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
   [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld() {

        string s = "Hello World";
       return new JavaScriptSerializer().Serialize(s);

    }

}

String Image

String return

0 个答案:

没有答案