doumu9019
2015-10-10 20:07
浏览 37
已采纳

无法从AngularJS $ http.post获取发布数据

Very new with AngularJS, I'm sure it's something simple I'm doing wrong but I cannot figure it out. All I'm trying to do is log the incoming POST from Angular, using PHP.

AngularJS function:

$scope.addTask = function() {
        $http.post('process.php', { newTask: $scope.newTask });
        $scope.newTask = '';
    };

process.php:

if (isset($_POST['newTask'])) {
        createLog('test');
    }

function createLog ($str) {
    $file = 'log.txt';
    $str .= "
";
    file_put_contents($file, $str, FILE_APPEND | LOCK_EX);
}

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

AngularJS非常新,我确信这很简单我做错了但我无法理解。 我要做的就是使用PHP从Angular登录传入的POST。

AngularJS函数:

  $ scope.addTask = function(){
 $ http.post('process.php',  {newTask:$ scope.newTask}); 
 $ scope.newTask =''; 
}; 
   
 
 

process.php: \ n

  if(isset($ _ POST ['newTask'])){
 createLog('test'); 
} 
 
function createLog($ str){
 $ file =  'log.txt'; 
 $ str。=“
”; 
 file_put_contents($ file,$ str,FILE_APPEND | LOCK_EX); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dsn5510 2015-10-10 20:30
    最佳回答

    If you are sending in a POST to PHP from angular, you need to retrieve it a bit differently in your backend. As noted by @charlietfl, the $_POST is empty. I have been out of the PHP game for a while but this should work for you.

    $data = file_get_contents("php://input");
    $request = json_decode($data);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题