如何使用php从ajax获取POST数据

如何使用php从ajax获取POST数据,php,ajax,angularjs,Php,Ajax,Angularjs,我在使用PHP和angular检索或发送POST数据时遇到问题。我将问题简化为最简单的情况,但响应仍然是一个空数组 这是我的角度代码: this.search = function() { console.log("searching"); $http({ method: 'POST', url: 'search/', data: {search:1, direct: true} }).then(func

我在使用PHP和angular检索或发送POST数据时遇到问题。我将问题简化为最简单的情况,但响应仍然是一个空数组

这是我的角度代码:

this.search = function() {
    console.log("searching");
    $http({
        method: 'POST',
        url: 'search/',
        data: {search:1, direct: true}
    }).then(function successCallback(response) {
        console.log(response.data);
    }, function errorCallback(response) {
        console.log("Error retrieving data.");
        console.log(response);
    });
}
和我的PHP代码:

<?php

header('Content-type: application/json');

echo json_encode($_POST) ;

?>


有趣的是,在这两个地方都使用GET,它是有效的:/

Angular将js对象序列化为JSON-to-POST,php不会从JSON-POST体填充$\u-POST数据结构,要在POST体中读取JSON数据,您必须从
php://input

<?php

header('Content-type: application/json');

echo file_get_contents('php://input') ;

?>

Angular将js对象序列化为JSON-to-POST,php不会从JSON-POST正文填充$\u-POST数据结构,要在POST正文中读取JSON数据,您必须从
php://input

<?php

header('Content-type: application/json');

echo file_get_contents('php://input') ;

?>

Angular将js对象序列化为JSON-to-POST,php不会从JSON-POST正文填充$\u-POST数据结构,要在POST正文中读取JSON数据,您必须从
php://input

<?php

header('Content-type: application/json');

echo file_get_contents('php://input') ;

?>

Angular将js对象序列化为JSON-to-POST,php不会从JSON-POST正文填充$\u-POST数据结构,要在POST正文中读取JSON数据,您必须从
php://input

<?php

header('Content-type: application/json');

echo file_get_contents('php://input') ;

?>


最有可能与最有可能与最有可能相关与最有可能相关与angular为什么将js对象作为json发送?可能是因为json比URL编码的表单数据更性感(只是猜测)。除非您以
application/x-www-form-urlencoded
multipart/form data
的形式发送数据,否则您不能使用$\u POST(除非您使用
$\u POST=json\u decode(文件获取内容('code])php://input“),true);
)。我不是角度专家,但是有一些方法可以让数据以表单数据的形式发送并使用$\u POST,但是我确实认为任何性能增益或损失都可以忽略不计。您如何将其转换为数组?我试过使用
json\u decode
,但是奇怪的事情发生了……您是否将json\u decode的第二个参数设置为true?为什么angular将js对象作为json发送?可能是因为json比URL编码的表单数据更性感(只是猜测)。除非您以
application/x-www-form-urlencoded
multipart/form data
的形式发送数据,否则您不能使用$\u POST(除非您使用
$\u POST=json\u decode(文件获取内容('code])php://input“),true);
)。我不是角度专家,但是有一些方法可以让数据以表单数据的形式发送并使用$\u POST,但是我确实认为任何性能增益或损失都可以忽略不计。您如何将其转换为数组?我试过使用
json\u decode
,但是奇怪的事情发生了……您是否将json\u decode的第二个参数设置为true?为什么angular将js对象作为json发送?可能是因为json比URL编码的表单数据更性感(只是猜测)。除非您以
application/x-www-form-urlencoded
multipart/form data
的形式发送数据,否则您不能使用$\u POST(除非您使用
$\u POST=json\u decode(文件获取内容('code])php://input“),true);
)。我不是角度专家,但是有一些方法可以让数据以表单数据的形式发送并使用$\u POST,但是我确实认为任何性能增益或损失都可以忽略不计。您如何将其转换为数组?我试过使用
json\u decode
,但是奇怪的事情发生了……您是否将json\u decode的第二个参数设置为true?为什么angular将js对象作为json发送?可能是因为json比URL编码的表单数据更性感(只是猜测)。除非您以
application/x-www-form-urlencoded
multipart/form data
的形式发送数据,否则您不能使用$\u POST(除非您使用
$\u POST=json\u decode(文件获取内容('code])php://input“),true);
)。我不是角度专家,但是有一些方法可以让数据以表单数据的形式发送并使用$\u POST,但是我确实认为任何性能增益或损失都可以忽略不计。您如何将其转换为数组?我试过使用
json\u decode
,但奇怪的事情发生了……您是否将json\u decode的第二个参数设置为true