4

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

3
  • 8
    What is resluts ? Commented Dec 25, 2012 at 10:12
  • i put a break-point at the Code Behind , i just couldn't even get the debugger to get to that point (like it does not fire ) . so two options , my code is a not correct, 2nd option my VS or Windows has issues or some bug Commented Dec 25, 2012 at 10:19
  • 1
    I am also interested in the resluts. Commented Dec 25, 2012 at 10:32

3 Answers 3

6

Modify your WebMethod like this and try again:

[WebMethod]
public static string GetSecondStageData(object sentobj)
{
    var x = sentobj;
    return DateTime.Now.ToString();
}
Sign up to request clarification or add additional context in comments.

4 Comments

@LoneXcoder: As you have return type as string for the GetSecondStageData web method, you need to return a string.
still not working , i just want to ask , as it is now(look at the time of this comment) should it work ? othewise i will try solve O.S and VS 2010 problems , like delete solution files or move project ...etc' ....thanks
ok just modify the data part like this data: "{ sentobj: " + SentPars + " }",
ha! , thanks Alot Dude . i allmost wanted to thank god (: really it was such a pain , i disslike js in Visual studio cause my lack of knoelege and lack of debugging utils
1

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.

3 Comments

i will try , thanks, u could have a look , now it's final update
My goodness I made a little mistake don't use $.parseJSON instead you should use JSON.stringify(test);
as it was solved with the help of @palash code in comment, and as he was the first to answer i chose to mark it as the correct answer . thanks alot for your help i will review your code again to learn some more. thanks
1

Please try below code

JavaScript

function UbpdateSecondStage(arr) {
    var WebMethod ="GetSecondStageData";
    var page ="Default.aspx/";
    var target = page + WebMethod;
    var SendParameters =arr;
    jQueryAjxUpdt(target, SendParameters);

}

function jQueryAjxUpdt(targetUrl, SentPars) {
          $.ajax({
              type: "POST",
              url: targetUrl,
              data: '{"results": "' + SentPars + '"}',
              contentType: "application/json; charset=utf-8",
              processData: false,
              dataType: "json",
              success: function(msg) {
              //alert(msg);
                 here your code
              },
              error: function(x, e) {
                  if (x.status == 500) {
                      alert("An error has occurred during processing your request.");
                  }
              }
          });
      }

C#

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

Let me know result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.