javascript - $http ajax AngularJS POST 在 PHP 中返回 undefined index

标签 javascript php jquery angularjs ajax

我通过 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/

相关文章:

javascript - 如何检查属性值是否等于oue值并获取该元素中另一个属性的值?

javascript - 如何在没有标签的地方添加标签?

java - Solr(php 与 java): query differences when determining number of documents

javascript 如何 foreach 循环遍历数组

javascript - 输入文本框不能用 jQuery Draggable 编辑?

javascript - 如何将悬停替换为单击

javascript - 在键入值或选择更改时连接值

javascript - 从 RGB 字符串中收集数字

php - 加载程序,它还显示网页加载的百分比

javascript - 组合两个输入事件