Actually I have a Array declared on JS side like below:
var benefArray = {};
var benefCount = 0;
var benefNome = $('#txtBenefNome').val();
var benefDataNasc = $('#txtBenefDataNasc').val();
var benefGrauParent = $('#txtBenefGrauParent').val();
benefCount++;
benefArray[benefCount] = new Array(benefNome, benefDataNasc, benefGrauParent);
//Ajax Sender
function sendAjax(url, parametros, sucesso) {
$.ajax({
type: "POST",
url: url,
data: parametros,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: sucesso
});
};
sendAjax("Client.aspx/AddClient", "{benefArray: \"" + benefArray + "\"}",
function (msg) {
var retorno = msg.d;
alert(retorno);
});
On my C# WebMethod side I Have:
[WebMethod]
public static string AddClient(object benefArray)
{
var t = benefArray;
}
I’m trying to get those values from Javascript, what I have to do?
Any insight on this will be appreciated! Thanks
Start by defining a model that will represent the data you are working with so that you work with strong types and get rid of the
objectugliness on yourAddClientmethod:then have your web method take an array of this model:
and on the client you would define an array of parameters:
As far as the
JSON.stringifymethod I am using here is concerned it is native in modern browsers. But if you intend to support older browsers it is recommended to include the json2.js script to your page.