views:

59

answers:

3

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>';
        });
        $('select[name^=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

A: 

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",
Popovich88
and on the other end, how do I access this in my web method?
Lyle
Request.QueryString["param"];
Popovich88
don't think its that easy, my web method doesn't understand Request.QueryString .. trying: string TorVName = System.Web.HttpContext.Current.Request.QueryString["TOrVName"].ToString(); with no luck :(
Lyle
A: 

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.");
  }
Kamyar
if you don't know what I want why did you post?
Lyle
@Lyle If you can't express what you want, why did you post a question? :)
bzlm
@Lyle, As I said, this was what I understood from you post. I thought you meant your code is complex and difficult and I wanted to offer a simpler way.
Kamyar
I could show you my web method but the data is privatized, however, I am passing back to the call an ArrayList SUCCESSFULLY. I don't understand your example(s) and I'm not rewriting my entire code because I can't get one string passed over json
Lyle
Well, I think you should study more on calling web methods from client side. There's no need to show me your web method. There's no need to rewrite your entire code. I guess you did not understand my sample at all. Anyway, good luck with using ArrayList.
Kamyar
all your separate functions are built in the $.ajax( call maybe you should study
Lyle
maybe you should see this:JSON returns 'undefined' when stringifying an object that was created in a different windowSee test case at http://kinsey.no/examples/ie8_json_bug/
Lyle
A: 

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.

cinqoTimo