0

I am using a web method and ajax call but can't get it right with parameters?

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?

Here is the ajax call:

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 \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.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"

6
  • Try with just jsdata in ajax Commented Jan 8, 2014 at 11:43
  • @SridharR i then get the error message: :"Invalid JSON primitive: Maintenance." Where Maintenance is my variable. Commented Jan 8, 2014 at 11:45
  • Refer this stackoverflow.com/questions/736058/…? Commented Jan 8, 2014 at 11:47
  • @SridharR I have seen that post and it did not provide any answers. Commented Jan 8, 2014 at 11:49
  • One answer explain the problem thats why it get up votes Commented Jan 8, 2014 at 11:50

1 Answer 1

1

your variable params var jsdata = '{category:' + category + '}' is a string.

So the line: JSON.stringify(jsdata, null, 2);, is redundant (or it should be). Just set data: 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);
      }
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Removing that line i then get the error message, "Message":"Invalid JSON primitive: Maintenance." Where Maintenance is the variable i was trying to pass in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.