无法发出$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
答案 0 :(得分:4)
这是一个已知问题Angular with 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;
答案 1 :(得分:1)
$post = json_decode(file_get_contents('php://input'), true);
我在mailer.php
中添加了以上单行,我的问题解决了。感谢所有回复。
答案 2 :(得分:0)
首先检查$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
});
答案 3 :(得分:0)
添加指令以设置所有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
});
}]);