无法将json值从angularjs发布到php

Not able to post json value from angularjs to php

本文关键字:php angularjs 值从 json      更新时间:2023-09-26

我已经用angular编写了一个服务,将值发布到PHP页面。它看起来像这样:

postValues: function(id,userImg) {
               return $http({ 
                      method: 'post',
                      url: 'phppage.php',
                      data: { id: 1,number: 3 },
                      dataType: 'json',
                      headers: {'Content-Type': 'application/json'}
                });
        }

我的PHP代码看起来像这样。

$id=$_POST['id'];
$no=$_POST['number'];

但我得到了未定义的索引错误。

然后我尝试了另一种方法:

$postdata       = file_get_contents("php://input");
$request        = json_decode($postdata);
$id             = $request->id;
$number         = $request->number;

但这也没有产生任何结果。

可能的原因是什么?这不是将值从angular POST到PHP的正确方法吗。

p.S:我还尝试过使用jQuery的$.praram()。

尝试发送这样的http请求,并使用promise捕获工厂函数中的响应

 postValues: function(id,userImg){
      $http({
          method: 'POST',
          url: 'phppage.php',
          data: { id: 1,number: 3 },
          headers: {'Content-Type': 'application/json'}
      }).then(function(response){
          return response
        },function(response){
           return response
        })
    }