传递参数的WebMethod使用jQuery阿贾克斯 [英] Passing parameter to WebMethod with jQuery Ajax

查看:242
本文介绍了传递参数的WebMethod使用jQuery阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Web方法和Ajax调用,并继续收到的说,它不能将字符串转换为一个IDictionary对象???

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???

下面是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的:

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

下面是我从萤火看到:

响应头结果
服务器:Microsoft-IIS / 5.1结果
日期:周四,2009年04月09日21时43分59秒GMT结果
jsonerror:真正的结果
缓存控制:私人结果
内容类型:应用程序/ JSON的;字符集= utf-8的结果
内容长度:1331

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 \\}

POST: "{\"ID\":\"4\"}"

响应:

{消息:无法将类型\\ u0027System.String \\ u0027对象键入\\ u0027System.Collections
.Generic.IDictionary`2 [System.String,System.Object的] \\ u0027,堆栈跟踪:在System.Web.Script.Serialization
.ObjectConverter.ConvertObjectToTypeInternal(对象o,类型类型的JavaScriptSerializer序列化,布尔
 throwOnError,对象和放大器; convertedObject)\\ r \\ n在System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain
(对象o,类型类型的JavaScriptSerializer序列化,布尔throwOnError,对象和放大器; convertedObject
)\\ r \\ n在System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(对象o,类型类型的JavaScriptSerializer
 串个)\\ 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)\\ r \\ n在System.Web.Script.Services.RestHandler
.GetRawParams(WebServiceMethodData methodData,HttpContext的背景下)\\ r \\ n在System.Web.Script.Services
.RestHandler.ExecuteWebServiceCall(HttpContext的背景下,WebServiceMethodData methodData),ExceptionType
:System.InvalidOperationException}

{"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"}

任何人对此有什么想法?谢谢!

Anyone have any ideas about this? Thanks!!

推荐答案

快速项目:

您的变量PARAMS
VAR PARAMS ='{ID:'+ ROWID +'}';
是一个字符串。

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

所以,该行:
数据:JSON.stringify(PARAMS)是多余的(或者应该是)。
只需设置数据:参数,可以

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

接下来,在你的Web方法,将您的结果转换为JSON字符串,并返回一个字符串。如果你的web方法类有ScriptMethod属性,你不需要做。
只需将数据返回为原生类型,Asp.Net会进行转换,以JSON你。

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.

您可以阅读以下条款:
<一href=\"http://elegant$c$c.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/\">http://elegant$c$c.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

<一个href=\"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/

这篇关于传递参数的WebMethod使用jQuery阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆