我通过 AngularJS 的 ajax $http
服务将数据发送到 API REST 服务,但 php 总是说索引未定义。我已经检查了很多关于 SO 的帖子,这些帖子都要求相同的错误,并且我尝试了他们所说的所有内容,但我仍然遇到相同的错误。
我如何使用 $http
服务:
$http({
method: 'POST',
url: window.location.pathname+'/../../api/tasks/'+$id,
data: {
'title': $scope.allData.taskData[0].title,
'content': $scope.allData.taskData[0].content,
'usersInTask': $scope.allData.taskUsers
},
contentType: 'application/json',
dataType: 'json',
headers: {'Content-Type': 'application/json'}
})
.then(function success(response){
$scope.closeDialog();
}, function error(response){
console.log("Error:" +response.statusText);
});
我如何尝试在 PHP 中获取值:
$title = $_POST['title'];
$content = $_POST['content'];
$users = $_POST['users'];
例如,在这种情况下,它总是说 title
是 $_POST
的未定义索引
最佳答案
要首先读取数据,您需要以 POST 格式对其进行解码。
$json_input = file_get_contents('php://input');
if ($json_input) {
$_REQUEST = json_decode($json_input, true);
}
关于javascript - $http ajax AngularJS POST 在 PHP 中返回 undefined index ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611760/