现在我有一个使用 $http 服务的基本发布请求。我在 console.log 中看到状态为 200(成功)的所有内容,但在 PHP 中没有运气。我试过使用 JSON_decode,我试过 $_POST (var_dump) 没有运气。如果您能想到解决方案,请提供帮助。
应用程序.js
var postData={
firstName:'something',
lastName:'here'
};
postData=JSON.stringify(postData);
$http({method:'POST',url:'sendmail.php',data:postData,headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
.success(function (data,status,headers,config) {
console.log('Success' + data);
})
.error(function (data, status,headers,config) {
// uh oh
console.log('error' + status + data);
});
发送邮件.php
$args = json_decode(file_get_contents("php://input"));
echo $args->firstName;
在我的 php 文件中出现以下错误。
注意:在第 3 行尝试获取/Applications/MAMP/htdocs/my-new-project/sendmail.php 中非对象的属性
但是,在我的 console.log 中我得到:
成功了
另外,我有一个存在同样问题的实时网站。
重申一下,无论我做什么,(添加 header 、JSON 解码)PHP 数组仍然为空。
最佳答案
当你在 AngularJS 中使用 $http.post() 时,你可以从 $_POST 数组中获取 PHP 中的参数,例如:
echo $_POST['firstName'];
如果 $_POST 是空的,那么你在其他地方有问题。
尝试隔离您的问题。 您可以使用 Postman、HTTPRequester 或任何其他 Firefox/Chrome 插件来模拟对 PHP 脚本的请求。
首先让您的 PHP 脚本工作,然后让您的 AngularJS 发布请求工作。
关于javascript - angularjs 使用 $http.post 到 PHP 但得到空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30601612/