<System.Web.Script.Services.ScriptService()> _
Public Class WebService
Inherits System.Web.Services.WebService
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function ReturnResults(ByVal id() As Object) As String
Dim returnResult As String = Nothing
Dim i As New JsonDataSet
'ReturnResults = i.GetData("spselahscinfo", CollectionItems("id"))
Return returnResult
End Function
the problem is if i receive the paramater as object or collection it give me below error
<div selected="selected" role="tabpanel">
{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Object[]\u0027","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
do i need to deserialize the paramater in the server, is it possible to manipulate it through collection. thanks in advance
I think it is a bit of an odd design choice to have a Script Method accept an array of object as its parameter value. Why do you specify it as an object rather than a class that has property "id" that is a string?
As the ScriptService is written in your initial post, returnResult is initialized to null and then never assigned to again before being returned. Has that changed since you posted? If not, make sure you assign a string to returnResult (and don't worry about
JSON serializing it; the ScriptService handler will do that for you).
sultani.khal...
Member
275 Points
198 Posts
passing parameter to webservice through jquery
May 05, 2010 04:22 AM|LINK
$('#AhscInfo').find('.select').bind('click',function(){
$.ajax({
type: "POST",
url: "WebService.asmx/ReturnResults",
data: "{'id':'863'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});
function OnSuccess(data)
{
}
});
this is my webserivce
<System.Web.Script.Services.ScriptService()> _
Public Class WebService
Inherits System.Web.Services.WebService
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function ReturnResults(ByVal id() As Object) As String
Dim returnResult As String = Nothing
Dim i As New JsonDataSet
'ReturnResults = i.GetData("spselahscinfo", CollectionItems("id"))
Return returnResult
End Function
the problem is if i receive the paramater as object or collection it give me below error
<div selected="selected" role="tabpanel"> </div>vcsjones
All-Star
34842 Points
4424 Posts
Moderator
MVP
Re: passing parameter to webservice through jquery
May 05, 2010 04:50 AM|LINK
I think it is a bit of an odd design choice to have a Script Method accept an array of object as its parameter value. Why do you specify it as an object rather than a class that has property "id" that is a string?
gt1329a
All-Star
15377 Points
2501 Posts
ASPInsiders
MVP
Re: passing parameter to webservice through jquery
May 05, 2010 06:50 AM|LINK
Change your input parameter's type to an Int (not an array of Int). The ScriptService will automatically deserialize the JSON for you.
A guide to combining jQuery and ASP.NET: jQuery for the ASP.NET developer
sultani.khal...
Member
275 Points
198 Posts
Re: passing parameter to webservice through jquery
May 05, 2010 11:44 AM|LINK
Thanks Dave for your reply, now i return results from web service as string but i get null,what might be the problem.
function OnSuccess(data)
<div id="sb5-L78" role="presentation"> { </div> <div breakpoint="true" id="sb5-L79" role="presentation">alert(data.d);</div> <div id="sb5-L80" role="presentation"> </div> <div id="sb5-L82" role="presentation"> } </div>
gt1329a
All-Star
15377 Points
2501 Posts
ASPInsiders
MVP
Re: passing parameter to webservice through jquery
May 05, 2010 03:23 PM|LINK
As the ScriptService is written in your initial post, returnResult is initialized to null and then never assigned to again before being returned. Has that changed since you posted? If not, make sure you assign a string to returnResult (and don't worry about JSON serializing it; the ScriptService handler will do that for you).
A guide to combining jQuery and ASP.NET: jQuery for the ASP.NET developer