在PHP中读取$http的内容


Reading content of $http in PHP

我将数据发送到服务器,如下所示:

$scope.saveCaption = function(user_id) {
  var target = document.getElementById('toRender');
      html2canvas(target, {
        onrendered: function(canvas) {
    $http({
      url: "/production/save.php?user_id="+user_id,
      method: "POST",
      headers: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        //image: canvas.toDataURL("image/png"), // commented out for testing only
        news: 'test'
        }
    }).success(function(data, status, headers, config) {
      console.log('success');
      $scope.data = data;
    }).error(function(data, status, headers, config) {
      console.log('failed');
      $scope.status = status;
    });
  }});
}

并尝试用PHP-save.php:阅读

$data = $_POST['news'];
echo "data is $data"; die;

问题是$_POST['news']总是空的?

这是发送的数据:

{"news":"test"} 

注意,它是JSON,但我特别尝试更改内容类型:

'Content-type': 'application/x-www-form-urlencoded'

那么,与JSON相比,我如何发送普通数据呢?或者,我如何让php正确读取JSON,我尝试了$data = json_decode($_POST['news']),但它也给出了空白

您需要对数据中的参数进行形式编码:

$scope.saveCaption = function(user_id) {
  var target = document.getElementById('toRender');
      html2canvas(target, {
        onrendered: function(canvas) {
    $http({
      url: "/production/save.php?user_id="+user_id,
      method: "POST",
      headers: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data: 'news=test', // form encoded
    }).success(function(data, status, headers, config) {
      console.log('success');
      $scope.data = data;
    }).error(function(data, status, headers, config) {
      console.log('failed');
      $scope.status = status;
    });
  }});
}