Passing parameter to WebMethod with jQuery Ajax

I have a simple web method and ajax call and continue to recieve an error that saying that it can’t convert a string to an IDictionary object???

Here is the ajax call:

var params = '{"ID":"' + rowid + '"}';
$.ajax({
  url: 'EDI.asmx/GetPartnerDetails',
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: JSON.stringify(params),
  dataType: "json", //not json . let me try to parse
  success: function(msg, st) { . . . .

Here is the webMethod:

<WebMethod()> _
Public Function GetPartnerDetails(ByVal ID As String) As String

    'Dim objParam As IDictionary = CType(JsonConvert.DeserializeObject(ID), IDictionary)
    'Dim rowID As String = objParam("ID")

    Dim objController As New HealthComp.BLL.X12Partners.TradingPartnersController
    Dim objInfo As TradingPartnersInfo = objController.FetchByPartnerID(Int32.Parse(ID))

    Return JsonConvert.SerializeObject(objInfo)
End Function

Here is what I see from FireBug:

Response Headers
Server: Microsoft-IIS/5.1
Date: Thu, 09 Apr 2009 21:43:59 GMT
jsonerror:true
Cache-Control:private
Content-Type:application/json; charset=utf-8
Content-Length:1331

POST: “{”ID”:”4”}”

RESPONSE:

{“Message”:”Cannot convert object of type u0027System.Stringu0027 to type u0027System.Collections
.Generic.IDictionary`2[System.String,System.Object]u0027″,”StackTrace”:” at System.Web.Script.Serialization
.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean
throwOnError, Object& convertedObject)rn at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain
(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject
)rn at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer
serializer)rn at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer
serializer, String input, Type type, Int32 depthLimit)rn at System.Web.Script.Serialization.JavaScriptSerializer
.Deserialize[T](String input)rn at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest
(HttpContext context, JavaScriptSerializer serializer)rn at System.Web.Script.Services.RestHandler
.GetRawParams(WebServiceMethodData methodData, HttpContext context)rn at System.Web.Script.Services
.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)”,”ExceptionType”
:”System.InvalidOperationException”}

Anyone have any ideas about this? Thanks!!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Quick item:

your variable params
var params = '{ID:' + rowid + '}';
is a string.

So the line:
data: JSON.stringify(params), is redundant (or it should be).
Just set data: params,

Next up, on your web method, you converting your result to a JSON string and returning that as a string. If you web method class has ScriptMethod attribute, you don’t need to do that.
Just return the data as the native type, and Asp.Net will do the conversion to JSON for you.

You might read the following articles:
http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

Method 2

In addition to the above, it is worth checking to make sure that you aren’t ‘stringifying’ the JSON array more than once.

I accidentally called JSON.stringify() on an array that had already been serialized, which threw a similar issue to the one the OP received.

i.e.

var arr = JSON.stringify({ id: elementID, name: Name });
....
$.ajax({
...
data: JSON.stringify(arr),
...
});

In this instance, changing the arr variable initialization to

var arr = { id: elementID, name: Name };

resolved my issue. 🙂


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x