jQuery/ajax POST an Array / objects to C# code behind

LoneXcoder picture LoneXcoder · Dec 25, 2012 · Viewed 7.4k times · Source

as i try to learn through other questions , still i cant get it to work

this is my code so far , trying to be as thorough as i could get .

the event (on click)

var resluts = []; //its a collections of id's - list items of unsorted list as strings 
$('#next').click(function() {
    var RLength = resluts.length;
    alert(resluts);
});​

ajax POST

function UbpdateSecondStage(arr) {

    var WebMethod ="GetSecondStageData";
    var page ="Default.aspx/";
    var target = page + WebMethod;
    var SendParameters = Sys.Serialization.JavaScriptSerializer.serialize(arr);
    jQueryAjxUpdt(target, SendParameters);

}


function jQueryAjxUpdt(targetUrl, SentPars) {
    $.ajax({
              type: 'POST',
              url: targetUrl,
              data: {
                     'sentobj':SentPars

              },
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (data) {
                                  //alert(data);
              }

   });
}

C#

  [WebMethod]
  public static string GetSecondStageData(object sentobj)
  {
      var x = sentobj;
    return ?? ...do i have to give a return.. even if i do not require one ?
  }

what is wrong with my code ?. it's first time i tried that approach . thanks.

I HAVE UPDATED FEW TIMES PLEASE READ IT AGAIN

enter image description here

Answer

palaѕн picture palaѕн · Dec 25, 2012

Modify your WebMethod like this and try again:

[WebMethod]
public static string GetSecondStageData(object sentobj)
{
    var x = sentobj;
    return DateTime.Now.ToString();
}