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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用POST方法将数据从我的登录FORM发送到PHP中编写的后端。

我的Angular代码如下所示:

  $ scope.getToken = function(){
 // console.log $ scope.login以确保 我不是发送空数据
 
 
 
 
 
 
 
 
 
 
 
 
控制台。
($ scope.login); 
 $ http({
方法:'POST',
 url:'../../ api / v1 / Ocuth。  php',
 data:{'login':$ scope.login,'password':$ scope.password} 
})。then(function successCallback(response){
 console.log(response); 
  ,函数errorCallback(响应){
 console.log(响应); 
}); 
}; 
   
 
 

之后我尝试抓住 它在我的PHP上:

  if((isset($ _ POST ['login'])AND isset($ _ POST ['password'])))
 {
  $ username = $ _POST ['login']; 
 $ password = $ _POST ['password']; 
 echo $ username; 
} 
else 
 var_dump($ _ POST); 
  <  / pre> 
 
 

此语句始终转到else并返回空数组。 Can s omeone告诉我我做错了什么或者如何调试它? 因为它看起来我正确地发送了数据,但它没有来到服务器。

谢谢 Kind 问候 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.

    点赞 打赏 评论
  • dongruo4601 2015-12-09 15:41

    try:

    data: { login : $scope.login, password : $scope.password }
    
    点赞 打赏 评论
  • dongluan1901 2015-12-09 15:48
    $http.post('url', {login: 'Alex', password: 'qwerty'}).then(function(){},function(){});
    
    点赞 打赏 评论
  • doujie9882 2015-12-09 15:52

    Some older server side web libraries like Coldfusion/.NET/PHP have issues grabbing a POST BODY by default (which is how $http sends the data).

    You can reference How to get body of a POST in php? to learn how to write your PHP in a way that it will accept the current and correct standard of sending data via a post.

    To access the entity body of a POST or PUT request (or any other HTTP method):

    $entityBody = file_get_contents('php://input');
    

    Also, the STDIN constant is an already-open stream to php://input, so you can alternatively do:

    $entityBody = stream_get_contents(STDIN);
    
    点赞 打赏 评论

相关推荐 更多相似问题