我有一個簡單的web方法和ajax調用,並繼續收到一個錯誤,指出它無法將字符串轉換爲IDictionary對象?將參數傳遞給WebMethod with jQuery Ajax
這裏是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
下面是我從螢火看到:
響應頭
服務器:Microsoft-IIS /5.1
日期:2009年4月9日星期四21:43:59 GMT
jsonerror:true
Cache-Control:private
Content-Type:application/json;字符集= UTF-8
內容長度:1331
POST: 「{\」 ID \ 「:\」 4 \ 「}」
RESPONSE:
{ 「消息」:「不能將\ u0027System.String \ u0027類型的對象轉換爲\ u0027System.Collections .Generic.IDictionary`2 [System.String,System.Object] \ u0027「,」StackTrace「:」at System.Web.Script.Serialization .ObjectConverter.ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer序列化程序,布爾型 throwOnError,Object & convertedObject)\ r \ n System.Web.Scri pt.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在System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest (HttpContext上下文,JavaScriptSerializer序列化程序)\ r \ n在System.Web.Script.Services.RestHandler .GetRawParams(WebServiceMethodData methodD ATA,HttpContext的背景下)\ r \ n在System.Web.Script.Services .RestHandler.ExecuteWebServiceCall(HttpContext的背景下,WebServiceMethodData methodData)」, 「ExceptionType」 : 「System.InvalidOperationException」}
任何人有任何想法對這個?謝謝!!
你是男人。非常感謝Chris!我添加了這個:var jsonText = JSON.stringify({ID:rowid});閱讀完文章後。我讀過大量的文章,顯示傳遞參數與報價。你是我讀過的第一個解釋並固定它的人。 Mucho gracias! – 2009-04-09 22:17:50
很高興有幫助。 – 2009-04-09 22:19:07