我有一个简单的web方法和ajax调用,并继续收到一个错误,说它无法将字符串转换为IDictionary对象?
这是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) { . . . .
这是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
这是我从FireBug中看到的内容:
响应标头 服务器:Microsoft-IIS / 5.1 日期:2009年4月9日,星期四,格林尼治标准时间 jsonerror:true 缓存控制:私有 内容类型:application / json; charset = utf-8 内容长度:1331
POST:“ {\” ID \“:\” 4 \“}”
响应:
{“消息”:“无法在系统上将类型为\ u0027System.String \ u0027的对象转换为类型为\ u0027System.Collections .Generic.IDictionary`2 [System.String,System.Object] \ u0027“,” StackTrace“:”。 Web.Script.Serialization .ObjectConverter.ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer序列化器,布尔throwOnError,Object&ConvertedObject)\ r \ n位于System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type type,JavaScriptSerializer序列化器,System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(对象o,类型类型,JavaScriptSerializer序列化器)的布尔型throwOnError,Object&convertObject)\ r \ n,System.Web.Script.Serialization.JavaScriptSerializer.Deserialize( JavaScriptSerializer序列化器,字符串输入,类型类型,Int32 depthLimit)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer .Deserialize [T](字符串输入)\ r \ n在System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化器)\在System.Web.Script.Services.RestHandler .GetRawParams(WebServiceMethodData methodData,HttpContext上下文)处为r \ n,在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)处为r \ n“,” ExceptionType“ :“ System.InvalidOperationException”}GetRawParams(WebServiceMethodData methodData,HttpContext上下文)\ r \ n位于System.Web.Script.Services .RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)“,” ExceptionType“:” System.InvalidOperationException“}GetRawParams(WebServiceMethodData methodData,HttpContext上下文)\ r \ n位于System.Web.Script.Services .RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)“,” ExceptionType“:” System.InvalidOperationException“}
有人对此有任何想法吗?谢谢!!
快速项目:
您的可变参数 var params = '{ID:' + rowid + '}'; 是一个字符串。
var params = '{ID:' + rowid + '}';
所以这行: data: JSON.stringify(params),是多余的(或者应该是)。刚设定data: params,
data: JSON.stringify(params),
data: params,
接下来,在您的Web方法上,将结果转换为JSON字符串并将其作为字符串返回。如果您的Web方法类具有ScriptMethod属性,则无需这样做。只需将数据作为本机类型返回,Asp.Net将为您完成向JSON的转换。
您可能会阅读以下文章: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/