TestWebService.asmx:
/// <summary>
/// TestWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class TestWebService : System.Web.Services.WebService
{
[WebMethod]
public int TestNoParams()
{
return 0;
}
[WebMethod]
public string TestWithParams(string a, int b)
{
return a + b.ToString();
}
}
aspx:
function PageMethod(fn, paramArray, successFn, errorFn) {
var pagePath = window.location.pathname;
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
$.ajax({
type: "POST",
url: "TestWebService.asmx" + "/" + fn,
contentType: "application/json;charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
});
}
PageMethod("TestWithParams", ["a", "value", "b", 2], AjaxSucceeded, AjaxFailed);
function AjaxSucceeded() {
alert('ok');
}
function AjaxFailed() {
alert("error");
}
不想用这个