part a) I m trying to send the value stored in the variable 'mem_ID' from my javascript page...default.aspx to my server side - default.aspx.cs page. But I keep getting an error message.
$.ajax({
type: "POST",
url: "default.aspx.cs",
data: "{mem_ID : ' " + mem_ID + "'}",
async: true,
// success: function (result) { }
});
$ - is undefined. Expected identifier or string.
part b) Also once i send this to the server side, how do i receive the value stored in the mem_ID ??
You could use a PageMethod
. Let's take an example of such a method in your code behind:
[WebMethod]
public static string MyMethod(string memId)
{
return string.Format("Thanks for calling me with id: " + memId);
}
Things to note: the method must be static and decorated with the [WebMethod]
attribute.
And on the client side you could invoke this method using the jQuery.ajax()
function like this:
$.ajax({
url: 'default.aspx/MyMethod',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ memID : mem_ID }),
success: function (result) {
alert(result.d);
}
});
Also the error you are getting about the undefined $
symbol is related to the fact that you didn't reference the jQuery library in your page. So make sure that in your WebForm you have actually added reference to the jQuery library before using it. For example:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>