c# – accessing the parameters of a JSON call in the web method – Education Career Blog

I don’t need anything fancy or complex, I’m just trying to pass a simple string across as a parameter to my web method. How can I do it?

Here is the web method call
WebMethod
public static ArrayList GetColumns(string TorVName)

here is the JSON call:

<script type="text/javascript" language="javascript"> 
  var qs = new Querystring();
  var v1 = qs.get("TorVName");
  var jsonData = JSON.stringify(v1);  
        $().ready(function() {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetColumns",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
       var optString = '<option value="-1">Select Column</option>';

      $.each(msg.d, function(index, item) {
        optString += '<option value="' + item + '">' + item + '</option>';
        });
        $('selectname^=DDLColumns').html(optString);
                },
                error: function() {
                    alert("Failed to load columns");
                }
            });
        });
</script>

Here is the essence of my web method:

   public static ArrayList GetColumns(string TorVName)
   {
        String cnstr = "myconnectstring";
        //string TorVName = System.Web.HttpContext.Current.Request.QueryString"TableOrViewName";
        //string TorVName = "Aged";
        //JavaScriptSerializer serializer = new JavaScriptSerializer();
        string TorVName = System.Web.HttpContext.Current.Request.QueryString"TOrVName".ToString();
        string Sql = String.Empty;

I think its stupid and disheartening that this needs to be so complex and difficult.

Thanks Dean

,

In your ajax request, for the data parameter, do it like this:

data: "myData=" + jsonData,

Then in your web method, match the argument to “myData” like this:

WebMethod()
public static ArrayList GetColumns(string myData)
{
 .....Your code
}

Your web method is smart enough to match the parameter to the argument if the names are the same. Once you receive your string, then you can deserialize it and call your non-webmethod to instantiate your custom data object.

,

If I understand what you want you just need to append the query string to your url in your ajax call:

url: "Default.aspx/GetColumns?param=value",

,

I could not understand what you want exactly. Anyway from my point of view, I see you’re using JQuery. I have written a JQuery plugin to call ajax methods easier. you can download it from: http://www.4shared.com/file/Y72VBeLc/PageMethod.html
Usage:
$.PageMethod('PageMethodName', successFunction, failFunction, 'param1', param1, 'param2', param2, ...);
An example:

Javascript:

var myName =  'TestName';
var myId = '123';
$.PageMethod('ReturnCustomData', onSuccess, onFail, 'MyName', myName,'MyID', myId);

Code Behind:

public class CustomData
{
    public string name;
    public int id;
}

WebMethod()
public static CustomData ReturnCustomData(string MyName, int MyID)
{
    CustomData MyData = new CustomData();
    MyData.name = MyName;
    MyData.id = MyID;
    return MyData;
}

The success function can have a serialized json object as a return value. in your callback function, you can access the CustomData

 function onSuccess(response)
  {
      alert('Your Name: ' + response.d.name + ', Your ID: ' + response.d.id);
  }
  function onFail(response)
  {
      alert("An error occurred.");
  }

Leave a Comment