locked
Passing Json Object to codebehind in c# RRS feed

  • Question

  • User-619898029 posted

    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

    Tuesday, June 21, 2016 6:17 AM

Answers

  • User1724605321 posted

    Hi mds,

    To  send and receive JavaScript JSON objects to Web Methods using jQuery AJAX , you could modify your code as :

      function onFinish() {
    
                var Myobj = {
    
                    FirstName: 'John',
                    LastName: 'Smith'
                };
    
           
                var myData = JSON.stringify(Myobj);
    
                $.ajax({
    
                    url: 'WebForm14.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');
    
                    }
    
    
                });
            }

    Code behind :

            [System.Web.Services.WebMethod]
            public static string Register(Person obj)
            {
              
    
                return "1";
            }

    Please refer to below article for more details about Ajax :

    http://api.jquery.com/jquery.ajax/ 

    http://www.aspsnippets.com/Articles/Send-and-Receive-JSON-objects-to-Web-Service-Methods-using-jQuery-AJAX-in-ASPNet.aspx  

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 21, 2016 7:49 AM