简单的json返回值asp.net与webservices我将显示我尝试的代码
当Button单击时,它会在json中返回一些值
<input type="button" id="btnSubmit" value="Submit" />
$(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);
}
});
});
});
[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);
}
}