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???
我有一个简单的web方法和ajax调用,并继续接收一个错误,说它不能将字符串转换为IDictionary对象?
Here is the ajax call:
以下是ajax调用:
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:
<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:
以下是我从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
响应头服务器:Microsoft-IIS/5.1日期:Thu, 2009年4月9日21:43:59 GMT jsonerror:真正的Cache-Control:私有内容类型:application/json;内容长度charset = utf - 8:1331
POST: "{\"ID\":\"4\"}"
帖子:“{ \“ID \”,\“4 \“}”
RESPONSE:
回应:
{"Message":"Cannot convert object of type \u0027System.String\u0027 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)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain (Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject )\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer .Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest (HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler .GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services .RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType" :"System.InvalidOperationException"}
{“消息”:不能转换类型\u0027系统的对象。字符串\ u0027 \ u0027System类型。System . string,集合.Generic.IDictionary ' 2(系统。对象]\ u0027”、“加”:“在System.Web.Script。序列化.ObjectConverter。ConvertObjectToTypeInternal(Object o, Type Type, JavaScriptSerializer serializer serializer serializer serializer, Boolean throwOnError, object&convertedobject)\ n at system . web.script.serializ.objectconverter。ConvertObjectToTypeMain (Object o, Type, JavaScriptSerializer serializer serializer serializer serializer serializer serializer, Boolean throwOnError, object&convertedobject)\ n at system . web . script.serializ.objectconverter。ConvertObjectToType(Object o, Type, JavaScriptSerializer serializer serializer)\r\n at system . web.script.serializ.javascriptserializer。反序列化(JavaScriptSerializer序列化器、字符串输入、类型、Int32 depthLimit)\r\n在System.Web.Script.Serialization。反序列化[T](字符串输入)\r\n在System.Web.Script.Services.RestHandler。来自postrequest (HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services。getrawparams (WebServiceMethodData methodData, HttpContext)\r\n at System.Web.Script。.RestHandler服务。ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)”、“ExceptionType”:“System.InvalidOperationException”}
Anyone have any ideas about this? Thanks!!
有人知道吗?谢谢! !
2 个解决方案
#1
31
Quick item:
快速的项目:
your variable params var params = '{ID:' + rowid + '}';
is a string.
变量params var params = '{ID:' + rowid + '}';是一个字符串。
So the line: data: JSON.stringify(params),
is redundant (or it should be). Just set data: params,
因此行:data: JSON.stringify(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.
接下来,在web方法中,将结果转换为JSON字符串并将其作为字符串返回。如果web方法类具有ScriptMethod属性,则不需要这样做。只返回作为本机类型的数据和Asp。Net将为您进行JSON的转换。
You might read the following articles: http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/
您可能会阅读以下文章:http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to- aspnet webmethod/
http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
#2
1
In addition to the above, it is worth checking to make sure that you aren't 'stringifying' the JSON array more than once.
除了上面的内容之外,还需要检查一下,以确保您不会不止一次地“字符串化”JSON数组。
I accidentally called JSON.stringify() on an array that had already been serialized, which threw a similar issue to the one the OP received.
我不小心在已经序列化的数组上调用了JSON.stringify(),这与OP收到的问题类似。
i.e.
即。
var arr = JSON.stringify({ id: elementID, name: Name });
....
$.ajax({
...
data: JSON.stringify(arr),
...
});
In this instance, changing the arr variable initialization to
在本例中,将arr变量初始化更改为
var arr = { id: elementID, name: Name };
resolved my issue. :)
解决我的问题。:)
#1
31
Quick item:
快速的项目:
your variable params var params = '{ID:' + rowid + '}';
is a string.
变量params var params = '{ID:' + rowid + '}';是一个字符串。
So the line: data: JSON.stringify(params),
is redundant (or it should be). Just set data: params,
因此行:data: JSON.stringify(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.
接下来,在web方法中,将结果转换为JSON字符串并将其作为字符串返回。如果web方法类具有ScriptMethod属性,则不需要这样做。只返回作为本机类型的数据和Asp。Net将为您进行JSON的转换。
You might read the following articles: http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/
您可能会阅读以下文章:http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to- aspnet webmethod/
http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
#2
1
In addition to the above, it is worth checking to make sure that you aren't 'stringifying' the JSON array more than once.
除了上面的内容之外,还需要检查一下,以确保您不会不止一次地“字符串化”JSON数组。
I accidentally called JSON.stringify() on an array that had already been serialized, which threw a similar issue to the one the OP received.
我不小心在已经序列化的数组上调用了JSON.stringify(),这与OP收到的问题类似。
i.e.
即。
var arr = JSON.stringify({ id: elementID, name: Name });
....
$.ajax({
...
data: JSON.stringify(arr),
...
});
In this instance, changing the arr variable initialization to
在本例中,将arr变量初始化更改为
var arr = { id: elementID, name: Name };
resolved my issue. :)
解决我的问题。:)