ECO Json

by A B

HTML

<script src="http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js"></script>
<button id='buttonJSON'>Echo JSON</button>
<div id='result'></div>

JavaScript

$('#buttonJSON').click(function() {
    /**
    * create a data object to send to the server, it must have two properties
    * json: the encoded (i use jQuery - Json library to do that) JSON that you will receive back
    * delay: how long the server waits before responding in seconds
    */
    var data = {
        json: $.toJSON({
            text: 'Echo JSON'
        }),
        delay: 1
    }
    $.ajax({
        //post the request to /echo/json/ and specify the correct datatype
        url: '/echo/json/',
        dataType: 'json',
        data: data,
        success: function(data) {
            //you get back exactly what you sent in the json 
            // alert(data.text);
            $('#result').html(data.text);
        },
        type: 'POST'
    });
});