簡體   English   中英

角$ http發布請求在PHP中未定義

[英]Angular $http post request undefined in PHP

無法發出$http發布請求,無法為php中的$_POST["name"]和所有其他發布的數據定義。 但是在我的控制台中正確打印了所有數據,您能幫我弄錯哪里嗎? 觸發點擊事件時,我正在發送數據,我對angular並不陌生,請幫助我解決此問題,感謝提前答復。

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

這是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上方添加了此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發布請求設置為表單發布。 您的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
        });
    }]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM