How do I pass JavaScript Object to my WebMethod on my aspx.cs  using ASPX webforms?

rich brown
rich brown used Ask the Experts™
on
I can pass javascript with 1 arg to WebMethod  on my aspx webform but I am unable to pass javascript object to my codebehind WebMethod. What's can I do to pass a Object to my aspx.cs  WebMethod? Get get the 500 status error when I click the btn.


This work.....
//==============
My ajax1:
//===============
function Show1() {
   
    $.ajax({
        type: "POST",
        url: "MyPage.aspx/GetCurrentTime",
        data: '{name: "' + document.getElementById("txtUserName").value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}




My Web Method:

      [WebMethod]
        public static string GetCurrentTime(string name)
        {
            return "Hello " + name + Environment.NewLine + "The Current Time is: "
                + DateTime.Now.ToString();
        }





This does not work....

//==============
My ajax2:
//===============
function Show2() {

    var val = { FirstName: "Mark", LastName: "Jones" };
    var iParam = { Info: val };

    var iUrl = "MyPage.aspx/Test";


    $.ajax({
        url: iUrl,
        data: JSON.stringify(iParam),
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        dataType: 'html',      
       success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}



//==============
My Web Method2:
//===============

       [WebMethod]
        public static string Test(TestMe info)
        {

            var i = info;
            string msg = string.Empty;
       

            return msg;

        }
//===============
My class is:
//===============
public class TestMe
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

    }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
what about :
[WebMethod]
        public static string Test(string FirstName, string LastName) 

Open in new window


or
 [WebMethod]
        public static string Test(TestMe info) 

Open in new window


with (javascript) so it match with the variable name :

data: JSON.stringify({info:iParam}), 

Open in new window

Author

Commented:
Change the case works - Thanks
leakim971Multitechnician
Top Expert 2014

Commented:
you welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial