Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Passing Json Object to codebehind in c#

$
0
0

Hi All,

I am working on a registration form where I have few fileds that need to be passed to codebehind using Jquery Ajax. I am using normal webforms. I have created a Person class and below is the class definition

public class Person
{
public string FirstName {get; set;}
public string LastName {get; set;}

public Activities PersonActivities {get;set}

}

public class Activities{

public string ActivityName {get; set;}

public datetime StartDate {get; set;}

}

I have created below ajax function on a button click event and below is the code.

function onFinish() {

                var Myobj = {

                    FirstName: 'John',
                    LastName: 'Smith'
                };

                var myData = JSON.stringify(Myobj);

                $.ajax({

                    url: 'PerosnReg.aspx/Register',
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: { obj: myData },
                    success: function (result) {
                        alert('sucess');
                    },

                    error: function () {
                        alert('Error');
                        
                    },
                    failure: function () {
                        alert('Failure');
                        
                    }


                });
            }

And below is my codebehind function, which I have written just to test my code is working without any logic in it.

[System.Web.Services.WebMethod]
public static string Register(Person obj)
{
   return "This is test!!" + obj.FirstName;
}

Below is the observation that I have made while executing the the above code

1. If I am not passing any data, then the exectuion comes to codebehind and i get the alert as 'Sucess'

2. If am trying to pass an JSON object, the I am getting an  500 Internal Error. 

Please guide me on the same

Thanks,

mds2907


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>