以下是代码:
$http({
method : 'POST',
url : 'mailer.php',
data : {fullname: "a", email: "b@c.c", confirmEmail: "a@ab.c", phone: "09", website: "a.c"} , // pass in data as strings
})
.success(function(data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
if (!data.success) {
// if not successful, bind errors to error variables
console.log('sending email error');
} else {
// if successful, bind success message to message
// $scope.message = data.message;
console.log('email sent');
}
})
我是angular和php的新手,所以请对我放松。:)
PHP代码:
$mailSend = null;
if (isset($_POST['email'])) {
$fullname = (isset($_POST['fullname'])) ? $_POST['fullname'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
$confirmEmail = (isset($_POST['confirmEmail'])) ? $_POST['confirmEmail'] : '';
$phone = (isset($_POST['phone'])) ? $_POST['phone'] : '';
$website = (isset($_POST['website'])) ? $_POST['website'] : '';
if (strlen($fullname) > 0 && strlen($email) > 0) {
$to = 'adrian@example.com';
$subject = 'Pricing Request';
$header = "From: " . strip_tags($email) . "'r'n";
$header .= "Reply-To: ". strip_tags($email) . "'r'n";
$header .= "MIME-Version: 1.0'r'n";
$header .= "Content-Type: text/html; charset=UTF-8";
$content = '<strong>Full Name: </strong>' . $fullname;
$content .= '<br/><strong>E-mail: </strong>' . $email;
$content .= '<br/><strong>Phone: </strong>' . $phone;
$content .= '<br/><strong>Website: </strong>' . $website;
$mailSend = (mail($to, $subject, $content, $header));
}
}
控制台输出:
200
sending email error
您永远不会返回具有success
属性的对象。。。这就是为什么data.success
是假的(因为它根本不存在):
if (!data.success) {
// if not successful, bind errors to error variables
console.log('sending email error');
}
但是这个请求看起来还可以,你甚至会得到一个200
响应状态,意思是"成功了!"。