简体   繁体   English

使用jQuery Ajax将参数传递给WebMethod时出错

[英]Passing parameter to WebMethod with jQuery Ajax getting error

I am using a web method and ajax call but can't get it right with parameters? 我正在使用Web方法和Ajax调用,但无法通过参数正确地进行操作吗?

I have worked though a number of fixings but non have been able to solve my problem? 我已经做了很多修复工作,但是还不能解决我的问题?

I need to pass though a string and have my web method return a data table, why is it necessary to pass it through as a json? 我需要通过字符串并让我的Web方法返回数据表,为什么有必要将其作为json传递呢?

Here is the ajax call: 这是ajax调用:

var jsdata = '{category:' + category + '}';
var jstext = JSON.stringify(jsdata, null, 2);
$.ajax({
  type: "POST",
  url: "GIFacRequest.aspx/GetSubCategories",
  data: jstext ,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (dtSubCategory) {
        PopulateSubCategoryDD(dtSubCategory);
      },
      error: function (response) {
         $('body', document).html(response.responseText);
      }
  });

And my webmethod: 而我的网络方法:

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static DataTable GetSubCategories(string category)
{
}

The error i'm getting is as follows: 我得到的错误如下:

"Message":"Cannot convert object of type \'System.String\' to type \'System.Collections.Generic.IDictionary`2[System.String,System.Object]\'","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.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, HttpContex “消息”:“无法将类型\\ u0027System.String \\ u0027的对象转换为类型\\ u0027System.Collections.Generic.IDictionary`2 [System.String,System.Object] \\ u0027”,“ StackTrace”:“在System.Web .Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object&convertedObject)\\ r \\ n位于System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type type,JavaScriptSerializer序列化器,在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化器,字符串输入,类型类型,Int32 depthLimit)处的布尔throwOnError,Object&convertObject)\\ r \\ n在System.Web.Script.Serialization.JavaScriptSerializer处。在System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化器)处反序列化[T](String输入)\\ r \\ n在System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData处) ,HttpContex t context)\\r\\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException" t context)\\ r \\ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)“,” ExceptionType“:” System.InvalidOperationException“

your variable params var jsdata = '{category:' + category + '}' is a string. 您的变量params var jsdata = '{category:' + category + '}'是一个字符串。

So the line: JSON.stringify(jsdata, null, 2); 所以这行: JSON.stringify(jsdata, null, 2); , is redundant (or it should be). ,是多余的(或者应该是)。 Just set data: jsdata, 只需设置数据:jsdata,

Try with this code 尝试使用此代码

var jsdata = '{category:' + category + '}';
$.ajax({
  type: "POST",
  url: "GIFacRequest.aspx/GetSubCategories",
  data: jsdata ,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (dtSubCategory) {
        PopulateSubCategoryDD(dtSubCategory);
      },
      error: function (response) {
         $('body', document).html(response.responseText);
      }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM