我完全困惑了。我有一个 WebMethod,我想从 javascript 调用它。很简单,用 jQuery,我以前已经这样做过。但这一次,我收到一条错误消息,指出我没有提供 WebMethod 的参数。但在我看来,我是。
我的 aspx 代码隐藏中有这个:
<WebMethod(True)> _
<ScriptMethod(UseHttpGet:=True)> _
Public Shared Function GetTimes(ByVal input as String) As Object()
Dim result As New List(Of Object)
result.Add(New With {.Text = "5:30", .Value = "1"})
result.Add(New With {.Text = "6:00", .Value = "2"})
result.Add(New With {.Text = "6:30", .Value = "3"})
result.Add(New With {.Text = "7:00", .Value = "4"})
Return result.ToArray
End Function
这在我的 JavaScript 中:
$.ajax({
url: 'ThePage.aspx/GetTimes',
contentType: "application/json; charset=utf-8",
data: '{"input":"test"}',
dataType: 'json',
succes: function (result) {
alert('yep');
},
error: function (request, errorType, obj) {
alert('nope');
}
});
但是我收到以下错误:
Invalid web service call, missing value for parameter: \u0027input\u0027.
包含的 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
有人见过这个吗?
最佳答案
您似乎正在将文字对象作为字符串。数据应为 {input:"test"}
而不是 '{"input":"test"}'
所以正确的调用是
$.ajax({
url: 'ThePage.aspx/GetTimes',
contentType: "application/json; charset=utf-8",
data: {input:"test"},
dataType: 'json',
succes: function (result) {
alert('yep');
},
error: function (request, errorType, obj) {
alert('nope');
}
});
关于jquery - 无效的 Web 服务调用,缺少参数值,但我将其包含在调用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505680/