Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json ajax with parameter passing

function BindJson() {
        $.ajax({
            type: "POST",
            url: "NewPage.aspx/SerializeJson",
            data: "{}",
            contentType: "application/json",
            dataType: "json",
            success: function (data1) {
                alert(data1);
            }
        })
    }
    [WebMethod]
        public static string SerializeJson()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            //Person p2 = js.Deserialize<Person>(str);
            return "";
        }

How do I pass parameters as data to my serializeJson function?

like image 339
sly_Chandan Avatar asked Jun 30 '11 12:06

sly_Chandan


People also ask

How pass json data to ajax to controller?

Create target "JSON object Mapper" object class file according to the business requirements. Create a "Controllers\HomeController. cs" file with default Index method and GetData(...) method with string type input query parameters for Ajax call with following lines of code i.e.

How pass JSON object in POST request in MVC?

Make sure you specify POST type, as ajax method uses GET method by default. MVC Controller: Decorate the Action method with HttpPost verb. This action method will only handle http post request from browser. Ajax submission from the browser will be automatically deserialized to FormData c# class as a poco.


1 Answers

This will work for you (full working code sample below). The key is to pass in a Person object. Also, I used a simple web service (myService.asmx) instead of an aspx page. Why bother with the extra overhead if it isn't needed?

The key is, on the client, create a Person object and then use JSON.stringify to pass the Person object to the webservice.

Javascript

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script>
<script type="text/javascript">
    function BindJson() {
        $.ajax({
            type: "POST",
            url: "myService.asmx/SerializeJson",
            data: JSON.stringify({ person:{ firstName: "Denny", lastName: "Cherian", department: "Microsoft PSS", address: { addressline1: "Microsoft India GTSC", addressline2: "PSS - DSI", city: "Bangalore", state: "Karnataka", country: "India", pin: "560028" }, technologies: ["IIS", "ASP.NET", "JavaScript", "AJAX"] }}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data1) {
                alert(data1.d);
            },
            error: function (request, status, errorThrown) {
                alert(status);
            }
        });
    }

    $(document).ready(function() {
        BindJson();    
    });
</script>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TestProject
{
    /// <summary>
    /// Summary description for myService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class myService : System.Web.Services.WebService
    {

        [WebMethod]
        public string SerializeJson(Person person)
        {
            return "Success";
        }

        public class Person
        {
            public string firstName { get; set; }
            public string lastName { get; set; }
            public string department { get; set; }
            public Address address { get; set; }
            public string[] technologies { get; set; }
        }

        public class Address
        {
            public string addressline1 { get; set; }
            public string addressline2 { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public string country { get; set; }
            public string pin { get; set; }            
        }
    }
}
like image 125
NakedBrunch Avatar answered Nov 21 '22 07:11

NakedBrunch