使用AJAX将对象从Javascript传递到PHP

时间:2014-09-22 08:20:22

标签: javascript php ajax json object

对象

var obj = [{id: 1, name: "Peter", lastname: "Griffin"}]

使用Javascript:

$.ajax({
    method: 'post',
    url:'http://api/test.php',
    data: {     
        func: "addtodatabase",
        objectToBePassed: obj   //also tried JSON.stringify(obj)
    },          
    success: function(data) {
       //code here
    }
});

PHP:

$data = json_decode($_REQUEST['objectToBePassed'], true);

我似乎无法使其发挥作用。帮助

1 个答案:

答案 0 :(得分:2)

不,你似乎对此感到困惑。你不需要json_decode()那个。一旦它在邮政体内。只需按原样访问它:

$data = $_POST['objectToBePassed'];

Here is a demo