duagfgfn1981 2015-12-09 15:39
浏览 43
已采纳

Angular - 将HTTP发布数据发送到服务器[HOW TO]

I'm trying to send data from my login FORM to backend writen in PHP using POST method.

my Angular code looks like:

$scope.getToken = function(){
    // console.log $scope.login to make sure I'm not sending empty data
    console.log($scope.login);
    $http({
        method: 'POST',
        url: '../../api/v1/Oauth.php',
        data: { 'login' : $scope.login, 'password' : $scope.password }
    }).then(function successCallback(response) {
        console.log(response);
    }, function errorCallback(response) {
       console.log(response);
    });
};

and after that I try to catch it on my PHP:

if((isset($_POST['login']) AND isset($_POST['password'])))
{
    $username = $_POST['login'];
    $password = $_POST['password'];
    echo $username;
}
else
    var_dump($_POST);

This statement always go to else and return empty array. Can someone advise me what I'm doing wrong or how can I debug this? Because it looks that I send data fron angular correctly but it didn't come to server.

Thanks Kind Regards Andurit

  • 写回答

4条回答 默认 最新

  • doushi3202 2015-12-09 15:48
    关注

    Use this:

    json_decode(file_get_contents('php://input'));
    

    Check your network tab in your developer bar. You can see that you send payload data in the http body. That's why the $_POST array is empty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 vc6.0中想运行代码的时候总是提示无法打开文件是怎么回事
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题