quarta-feira, 11 de julho de 2012

Send JSON Data using POST Method

 This sample, build a JSON and sent the data to server:



/*Retrieve data from table and sending to server using AJAX POST*/

 function SendMyJSON() {
        var myData= retrieveDataFromTable();
        $.ajax({
            type: "POST",
            url: 'http://www.yoursite.com.br/yourcontroler/action',
            contentType: 'application/json',
            dataType: 'json',
            async: false,
            data: JSON.stringify(myData),
            success: function(dataReturn) {
                alert(dataReturn);
            }
        });
    }
/*How to read a HTML Table*/
    function retrieveDataFromTable() {
        var col1= jQuery("#grid tr td.col1");
        var col2= jQuery("#grid tr td.col2");
        var affectObjects= [];
        var y = 0;
        for (var i = 0; i < col1.length; ++i) {
                affectObjects.push({ "col1": 0, "col2": ""});
                affectObjects[y].col1= col1[i].innerText ? $.trim(col1[i].innerText) : $.trim(col1[i].textContent);
                affectObjects[y].col2= col2[i].innerText ? $.trim(col2[i].innerText) : $.trim(col2[i].textContent);
                          y++;
            }
        }
        return affectObjects;
    }
Postar um comentário