I'm totally puzzeled. I have a WebMethod that I want to call from javascript. Simple enough, with jQuery, I've done this before. Yet this time, I get an error stating I didn't provide the argument for the WebMethod. But in my opinion, I am.
I have this in my aspx code-behind:
<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
And this in my javascript:
$.ajax({
url: 'ThePage.aspx/GetTimes',
contentType: "application/json; charset=utf-8",
data: '{"input":"test"}',
dataType: 'json',
su开发者_JAVA技巧cces: function (result) {
alert('yep');
},
error: function (request, errorType, obj) {
alert('nope');
}
});
Yet I get the following error:
Invalid web service call, missing value for parameter: \u0027input\u0027.
The StackTrace included is:
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
Anyone seen this before?
You seem are making the literal object as string. The data should be {input:"test"}
instead of '{"input":"test"}'
So the correct call will be
$.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');
}
});
We ended up using AjaxPro, which wasn't the preferred method, because it's no longer being actively developed. Plus, there's not enough documentation.
I tried the response from mohang, but got another error. I'm almost certain it's related to our specific project, because even the code in my question worked in another project.
精彩评论