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 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?