Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 593
LoneXcoder Avatar asked Feb 18 '23 01:02

LoneXcoder


2 Answers

Modify your WebMethod like this and try again:

[WebMethod]
public static string GetSecondStageData(object sentobj)
{
    var x = sentobj;
    return DateTime.Now.ToString();
}
like image 196
palaѕн Avatar answered Feb 20 '23 14:02

palaѕн


First of all you should have specified what you are passing ( sending to the server) and what response you are getting( if you are luck ;)) back from the server. If it's throwing or rasing any error then that should be specified as well.
Secondly there is no need to use the following code.

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

you can work like this as well

var test=new Object();
test.myArray=arr;
jQueryAjxUpdt("<Relative path to the directory such as '../home/default.aspx' >", JSON.stringify(test))

again change ajaxFunction as follows

function jQueryAjxUpdt(targetUrl, SentPars) {
$.ajax({
          type: 'POST',
          url: targetUrl,
          data: SentPars,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (data) {
                              //alert(data);
          }
    });
}

Haven't tested but I guess It will work let me know if don't. ;-)
And finally I guess you haven't kept anything in your default.aspx page other then @page directive and It works even if you don't specify [WebMethod] attribute.

like image 44
Hiren Desai Avatar answered Feb 20 '23 14:02

Hiren Desai