使用 jQuery Ajax 将参数传递给 WebMethod [英] Passing parameter to WebMethod with jQuery Ajax

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

问题描述

我有一个简单的 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

这是我从 FireBug 中看到的:

Here is what I see from FireBug:

响应头
服务器:Microsoft-IIS/5.1
日期:2009 年 4 月 9 日星期四 21:43:59 GMT
jsonerror:true
缓存控制:私有
内容类型:应用程序/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"}"

回复:

{"Message":"无法将 u0027System.Stringu0027 类型的对象转换为 u0027System.Collections 类型.Generic.IDictionary`2[System.String,System.Object]u0027","StackTrace":" 在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, BooleanthrowOnError, Object&ConvertObject) 在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer 序列化器, Boolean throwOnError, Object&convertedObject) 在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer序列化器) 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序、字符串输入、类型类型、Int32 depthLimit) 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input) 在 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext 上下文,JavaScriptSerializer 序列化程序) 在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context) 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

{"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) at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain (Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject ) at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) at System.Web.Script.Serialization.JavaScriptSerializer .Deserialize[T](String input) at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest (HttpContext context, JavaScriptSerializer serializer) at System.Web.Script.Services.RestHandler .GetRawParams(WebServiceMethodData methodData, HttpContext context) at System.Web.Script.Services .RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType" :"System.InvalidOperationException"}

有人对此有任何想法吗?谢谢!!

Anyone have any ideas about this? Thanks!!

推荐答案

快速项目:

你的变量参数var params = '{ID:' + rowid + '}';是一个字符串.

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

所以这行:data: JSON.stringify(params), 是多余的(或者应该是).只需设置 data: 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.

您可能会阅读以下文章:http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

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/

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

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