Send Json Array Through jQuery Ajax

CharlesDou picture CharlesDou · Jan 25, 2013 · Viewed 18.9k times · Source

I am going to send a array of json object through jquery ajax call to a php file.

var arr = new Array();
var record1 = {'a':'1','b':'2','c':'3'};
var record2 = {'d':'4','e':'5','f':'6'};
arr.push(record1);
arr.push(record2);

How can I send the array through jquery ajax? and how can I get the values in php? Thanks.

Answer

Felix Wessendorf picture Felix Wessendorf · Jan 25, 2013
$.ajax({
        url: "api",
        type: "POST",
        dataType: 'json',
        data: JSON.stringify(arr),
        success: function(response){}

       });

And with PHP:

$strRequest = file_get_contents('php://input');
$Request = json_decode($strRequest);