Reputation: 21
Please help me!!
I have some problem with Ajax in web form asp dotnet2010.
In .aspx file i have function:
$("#Result").click(function () {
$.ajax
({
type: "POST",
url: "Example1.aspx/GetDate",
beforeSend: function (xhr) {
// alert('Befor to send');
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
},
//data: "{}",
data: JSON.stringify({ v1: val1, v2: val2 }),
//data: '{"v1":"' + val1 + '","v2":"' + val2 + '"}',
//data: JSON.stringify(dataParam),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
}); // $.ajax
}); //$("#Result").click(function ()
GetDate() function in behind code(file .cs):
public static string GetDate(string v1, string v2)
{
string s2 = HttpContext.Current.Request.Form["v2"];
}
The first, I don't to get data from input parameter, But I want to get input parameter from HttpContext.Current.Request. i see value variable s2=null, Please help me!! How i get that value ???
The second, In case
public static string GetDate(string v1, string v2)
{
string s2 = HttpContext.Current.Request.Form["v2"];
}
and in ajax
data: { v1: val1, v2: val2 },
when i click button in form aspx , i don't call GetDate() function ?
Thanks
Upvotes: 2
Views: 120
Reputation: 21795
You first problem is that you have not included the WebMethod
attribute in your code behind method, first include that to make your AJAX call work like this:-
[WebMethod]
public static string GetDate(string v1, string v2)
{
string s2 = HttpContext.Current.Request.Form["v2"];
}
Your second issue in this line string s2 = HttpContext.Current.Request.Form["v2"];
is that you are trying to fetch the data from current request from collection but this is an Ajax call and you have passed the data in json format, so can simply fetch the data like this in your method:-
string s2 = v2;
Upvotes: 1