Pass json object to ASP.NET web service?

Last post 07-07-2009 9:34 AM by eacousineau. 2 replies.

Sort Posts:

  • Pass json object to ASP.NET web service?

    07-06-2009, 8:01 PM
    • Member
      334 point Member
    • Omego2K
    • Member since 06-11-2008, 3:34 AM
    • Posts 198

    Hi, I was wondering, out of sheer curiousity, how one would pass a json object to an ASP.NET web service using just XMLHttpRequest and no jquery or MS frameworks. I already know how I would get a json script back, but I was wondering how I would pass one in.

  • Re: Pass json object to ASP.NET web service?

    07-06-2009, 9:21 PM
    Answer
    • Member
      334 point Member
    • Omego2K
    • Member since 06-11-2008, 3:34 AM
    • Posts 198

    I actually figured it out. You basically pass in a json script to match the class in the parameter. For example:

    {"Name": "John Smith", "Race": "White", "IsBold": false}

    That needs to be in a string format of course. You send it using the following way:


    xhr3.onreadystatechange = getUniqueID;
            xhr3.open('GET', 'WallService.asmx/GetUniqueID', true);
            xhr3.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
            xhr3.send(null);


    xhr.onreadystatechange = OnSuccessFunction;

    xhr.open('GET', 'WebService.asmx/AddPerson?person={"Name": "John Smith", "Race": "White", "IsBold": false}', true);

    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');

    xhr.send(null);

    The WebService should of course have a method defined to receive the request, such as


    [WebMethod]

        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = true)]

        public void AddPerson(Person person)

        {

            SomeColletion.Add(Person);

        }

    And of course you must have a person class(structure may work too, not sure) with the corresponding properties as defined in the json with a default constructor

    public class Person

    {

    public Person(){}

    private string _name;

    public string Name

    {

    get{return _name;}

    set{_name = value;}

    }

    private string _race;


    public string Race

    {

    get{return _race;}

    set{_name = _race;}

    }


    private bool _bold;


    public bool Bold

    {

    get{return _bold;}

    set{_bold = value;}

    }


    }

    Now the web service method above has the script method attribute, I am not 100% sure if you need it, I didn't try without it. Maybe I should.




  • Re: Pass json object to ASP.NET web service?

    07-07-2009, 9:34 AM
    Answer
    • Member
      12 point Member
    • eacousineau
    • Member since 06-26-2009, 10:46 AM
    • Posts 4

    As a side note you should probably use escape() on your JSON object to sanitize the input if you're passing it through GET.

    Alternatively you can send the request using POST and just add it to the contents of your request.

Page 1 of 1 (3 items)