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
In your ajax request, for the data parameter, do it like this:
Then in your web method, match the argument to “myData” like this:
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.