Angular $http PHP 中未定义的后请求


无法

发出$http post 请求,在 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:xampphtdocswwwrootcontact-formfilescontact_mailer.php on line 16
[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: subject in C:xampphtdocswwwrootcontact-formfilescontact_mailer.php on line 17
[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: mail in C:xampphtdocswwwrootcontact-formfilescontact_mailer.php on line 20
[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: message in C:xampphtdocswwwrootcontact-formfilescontact_mailer.php on line 21
[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: name in C:xampphtdocswwwrootcontact-formfilescontact_mailer.php on line 21
[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: mail in C:xampphtdocswwwrootcontact-formfilescontact_mailer.php on line 21
[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: no in C:xampphtdocswwwrootcontact-formfilescontact_mailer.php on line 21

这是一个已知的问题 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;
$post = json_decode(file_get_contents('php://input'), true); 

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

在发送请求之前,首先检查$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
  });

添加一个指令,为所有 http post 请求设置为表单帖子。 您的 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
        });
    }]);

最新更新