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条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号