我正在使用AngularJS将一个复杂的JSON对象发布到运行PHP 5.4的Azure IIS中:
var request = $http({
method: "post",
url: "/php/mail.php",
data: $scope.contactForm,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
我的PHP脚本由这两行组成:
$data = json_decode(file_get_contents("php://input"));
print_r($data);
当尝试读取 POST 数据时,所有数组($_POST
、$_REQUEST
和 php://input
(都是空的。没有会杀死POST
数据的重定向,并且回显$_SERVER['CONTENT_TYPE']
返回'application/x-www-form-urlencoded'
。
由于您的 PHP 需要 JSON,因此您的 Content-Type 不正确。 此外,您需要字符串化数据,因此您的 AngularJS 代码如下所示:
var request = $http({
method: "post",
url: "/php/mail.php",
data: JSON.stringify($scope.contactForm),
headers: {
'Content-Type': 'application/json'
}
});