dream04110 2017-01-17 04:13
浏览 49
已采纳

angular-php:表单数据在php中为空

I am using angular as frontend and php as backend here is the code of angular.

$scope.processForm = function($scope.formData) {
    console.log($scope.formData);
    $http({
        method  : 'POST',
        url     : 'process.php',
        data    : {'data': $scope.formData,'uid':uid}, // no need to use $.param, even never see it in angular
        headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  
    })

here is the process.php

$postContent= file_get_contents("php://input"); 
$req= json_decode($postContent); 
$formData= $req->formData; 
$uid= $req->uid;

problem is $formData is empty in php. however $uid is showing value.

in form i have two entry email and password but i don't know how can i use that in php because formdata is empty.

I checked in firebug and found data is posting.

{"formData":{"password":"ff","cpassword":"errgreg"},"uid":"75"}:""

But nothing is coming response tab of firebug.

  • 写回答

2条回答 默认 最新

  • dongpiao9078 2017-01-17 04:46
    关注

    Assuming you call your function with something like ng-submit="processForm(formData)" then this is all you actually need

    $scope.processForm = function(formData) {
      $http.post('process.php', {
        formData: formData, // note the key is "formData", not "data"
        uid: uid // no idea where uid comes from
      });
    };
    

    Where you have

    $scope.processForm = function($scope.formData) {
    

    isn't even valid JavaScript. You cannot use object dot notation in function argument names. That should have been throwing an error in your console.


    You also appeared to be incorrectly setting your request content-type. You are sending JSON, not application/x-www-form-urlencoded formatted data. Angular's default POST content-type (application/json) is sufficient.

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

报告相同问题?