我的 ajax 调用突然停止工作,我不知道为什么。我没看到什么?
答:是因为ASP.NET State Server
关机了。
这是我的网络方法
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Test(string message)
{
return message;
}
这是我的ajax调用
$.ajax({
url: '/Test.asmx/Test',
data: {message:'hello'},
type: "POST",
async: true,
contentType: "application/json; charset=UTF-8",
dataType: "json",
beforeSendMethod: function (xhr) {
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
},
success: function (data, textStatus, jqXHR) {
console.log('success!',data, textStatus, jqXHR);
},
error: function (data, textStatus, jqXHR) {
console.log('error!',data, textStatus, jqXHR);
}
});
这是我的错误
Message: Invalid web service call, missing value for parameter: 'message'.
StackTrace: at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)
at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)
ExceptionType: System.InvalidOperationException
如果我这样做,它会起作用:
$.ajax({
url: '/Services/Authentication.asmx/Test',
data: {message:'hello'},
type: "POST",
success: function (data, textStatus, jqXHR) {
console.log('success!',data, textStatus, jqXHR);
},
error: function (data, textStatus, jqXHR) {
console.log('error!',data, textStatus, jqXHR);
}
});
除了 WebMethod 返回 XML 而不是 JSON:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">hello</string>