Post an Json object to an XML webservice

When working with complex object on the client it is nice to submit this ( Json ) object to the server by passing it as a parameter in a WebService method. On the server side the Json object is then automatically serialized to a .Net object wich can be processed without any intervention.

Like this the border between client-side and server-side code is a litle lower: by using on both sides the same object.

First thing is to enable the http methods in the web.config.

        <add name="HttpGet" />
        <add name="HttpPost"/>

Now create the client side code to post a Json object which is going to be saved in the database and receive a Json object with the result of the action:

// the complex object which is going
// to be serialized to a .Net object
var Sex = {
    "Id": "1"
    , "Name": "Test"
    , "Date":  new Date( 2015, 1,1)

// the package object which allows you to
// submit the complex object to the server
var SendSexObject = {
    "sex": Sex

// the url where the XML webservice is hosted
var urlResult = "http://localhost:49559/Services/JsonReceiver.asmx/PostSexWithResult"; 

Now the jQuery ajax method which is going to submit the objects together with the succeed and failes handling methods:

function SubmitJsonTestResult() {
    var request = $.ajax({
        type: "POST",
        url: urlResult,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(SendSexObject),
        dataType: "json",
        success: SubmitSucceeded,
        error: SubmitFailed

function SubmitSucceeded(data) {
    console.log("yahoo, het is gelukt");
    var result = data.d;
    // write the result
    // write the value
    // call the standard messagehandling

function SubmitFailed(data) {

So, as you can see, we have a Sex object on the client. This object is going to be wrapped in a a SendSexObject to enalbe it to send it to the server. On the server there is a webmethod: PostSexWithResult which receive this Json object and parses it to the .Net Sex object automatically, validate it and passed it the businessObject. The businessObject returns a .Net object ( returnMessageBase ) wich is returned to the client. The client uses this ReturnMessageBase class to detect if the action is succeeded or not and the error messages etc.:

/// <summary>
    /// Summary description for JsonReceiver
    /// </summary>
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    public class JsonReceiver : System.Web.Services.WebService

        public ReturnMessageBase PostSexWithResult(Sex sex)
            var returnMessage = new Common.ReturnMessageBase();
            // validate
            if (sex == null)
                returnMessage.Succeeded = false;
                returnMessage.Message = "Sex object cannot be null";
                return returnMessage;
            // process
            var businessSexObject = new Business.Sex();
            returnMessage = businessSexObject.PostSex(sex);

            return returnMessage;





Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!