dtcd27183 2015-04-04 05:12
浏览 11
已采纳

在PHP中读取$ http的内容

I'm sending data to the server as follow:

$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;
    });
  }});
}

And trying to read it with PHP - save.php:

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

The problem is that $_POST['news'] is always blank?

This is the data sent:

{"news":"test"} 

Notice it's JSON, yet I specifically tried to change the content type:

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

So how can I send normal data as opposed to JSON? Or, how can I get the php to read the JSON properly, I tried $data = json_decode($_POST['news']) but that gives blank too

  • 写回答

1条回答 默认 最新

  • duanjue9889 2015-04-04 07:06
    关注

    You need to form encode the parameters in data:

    $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;
        });
      }});
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?