Alcor s Alcor s - 2 months ago 7
JSON Question

receive JSON variables in Web Method

This is not a new idea in programming but I really need help on how to do it correctly. I am trying to send 2 variables in a JSON call to a web method.

$("#btn1").click(function () {
getEventL($(this).find(1).val(), "a");
});

function getEventLetter(v1, v2) {
var nEvent = { var1: v1, var2: v2 };
$.ajax({
type: "POST",
url: "default.aspx/getEventL",
data: JSON.stringify(nEvent),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
}
});
}


How can I receive and read these two values in the web method?

Can I do just this?

public static object getEventLetter(string var1, string var2)
{
// get event
int _var1= Int32.Parse(var1);
string _var2= var2;
}

Answer

What you are doing seems to be correct. You can simply get the data passed in ajax call by referencing the parameters from the web method as:

public static object getEventLetter(string var1, string var2)
{
 string v1 = var1;
 string v2 = var2;
 //other code...
}