【问题标题】:Angular $http post request undefined in PHPAngular $http post请求在PHP中未定义
【发布时间】:2015-07-08 06:51:54
【问题描述】:

无法发出$http 发布请求,无法在php 中为$_POST["name"] 和所有其他发布的数据定义。但是在我的控制台中正确打印所有数据,你能帮我在哪里出错吗?触发点击事件时我正在发送数据,我是角度的新手,请帮我解决这个问题,感谢提前回复。

angular.element(document.querySelector('#applyJob')).unbind('click').bind('click', function () {
    console.log($scope.userName+$scope.userEmail+$scope.userMobileNo+$scope.subject+$scope.userCoverLetter+$scope.attach);
    $http({
        method: "POST",
        url: "mailer.php",
        data: {
            name : $scope.userName,
            mail : $scope.userEmail,
            no : $scope.userMobileNo,
            subject : $scope.subject,
            message : $scope.userCoverLetter,
            attach : $scope.attach
        },
        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    });
});

我的php 代码如下所示

require ('smtp_lib/phpmailer.php');
require ('smtp_lib/smtp.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxx@gmail.com";
$mail->Password = "yyyyyy";
$mail->FromName = $_POST["name"];
$mail->Subject = $_POST["subject"];
$mail->AddAddress("zzz@gmail.com");
$mail->AddReplyTo($_POST["mail"]);
$mail->Body = $_POST["message"].'<br>'.$_POST["no"];
$mail->AddAttachment($_POST["attach"]);
$mail->Send();

如果我打开 php_error_log 我会收到这些错误

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: name in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 16

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: subject in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 17

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: mail in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 20

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: message in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: name in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: mail in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: no in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21

【问题讨论】:

  • 您的代码似乎是正确的,您能var_dump($_POST) 并分享结果
  • 里面没有任何线索。如果将 ajax 请求发送到服务器,您能否在控制台中检查,如果是,则将其作为表单发送,例如 blog.yiiframe.com/wp-content/uploads/2014/11/…
  • 您是否在邮递员或任何其他 REST 客户端中测试过您的 Web 服务。如果您确实在其中发布了您的问题的回复。
  • @yiiframe 我成功了,如果成功意味着数据已经发送到服务器了
  • 去掉多余的标头试试$post = json_decode(file_get_contents('php://input'), true); var_dump($post);

标签: javascript php ajax angularjs


【解决方案1】:

添加一个指令以设置为所有 http 发布请求的表单发布。您的 php 代码将保持不变

//Directive - change App with your app name
    App.factory("setAsFormPost", ['$rootScope', function($rootScope) {
        function transformRequest(data, getHeaders) {
            var headers = getHeaders();
            headers[ "Content-type" ] = "application/x-www-form-urlencoded; charset=utf-8";
            return($.param(data));
        }
        return(transformRequest);
    }]);

// you need to pass setAsFormPost to your controller like this
        App.controller('ControllerName',
        ['$scope','setAsFormPost', '$http', function($rootScope, $scope, messageHandler, settings, yiiBox, yiiHttp) {
        $http.post('mailer.php', {name : $scope.userName, mail : $scope.userEmail,
                no : $scope.userMobileNo, subject : $scope.subject, message : $scope.userCoverLetter,
                attach : $scope.attach}, {transformRequest: setAsFormPost}).success(function (result) {
            //something
        }).error(function (error) {
            //something
        });
    }]);

【讨论】:

    【解决方案2】:
    $post = json_decode(file_get_contents('php://input'), true); 
    

    我在mailer.php 中添加了上面的单行,我的问题解决了。感谢所有回复。

    【讨论】:

      【解决方案3】:

      这是 Angular 与 PHP 的一个已知问题,您可以使用它来解决;

      var form = {name:"x",mail:"x",no:0,subject:"x",message:"x",attach:"x"};
      var formData = new form(); 
      formData.name = $scope.userName,
      formData.mail = $scope.userEmail,
      formData.no = $scope.userMobileNo,
      formData.subject = $scope.subject,
      formData.message = $scope.userCoverLetter,
      formData.attach = $scope.attach
      
      $http({
              method: "POST",
              url: "mailer.php",
              data: formData
      });
      

      在您的 php 中,您使用 file_get_contents("php://input") ;

      $postdata = file_get_contents("php://input");
      $formData = json_decode($postdata);
      echo $formData->name;
      

      【讨论】:

      • 我不会说这是个问题,它只是在angular 中设置的默认content-typeapplication/json。您可以根据自己的需要和选择更改它们
      • @hurricane 我运行你的代码得到这样的错误Uncaught TypeError: Cannot set property 'name' of undefined
      • @Selva 实际上你需要为集合元素定义一个空对象。您可以使用 formData = new Object();或创建一个对象。
      【解决方案4】:

      在发送请求之前先检查$scope.userName是否在控制台中定义,你可以这样写:

      var data = {
                  name : $scope.userName,
                  mail : $scope.userEmail,
                  no : $scope.userMobileNo,
                  subject : $scope.subject,
                  message : $scope.userCoverLetter,
                  attach : $scope.attach  
                }
      
      $http.post('mailer.php', { params: data }).
        success(function(data, status, headers, config) {
          // something
        }).
        error(function(data, status, headers, config) {
          // something else
        });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-17
        • 2015-06-01
        • 1970-01-01
        • 2011-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        相关资源
        最近更新 更多