我不知道是不是问题,这是我的角色脚本将表单数据发送到PHP脚本的电子邮件。我试图从github访问代码示例,但这似乎没有对我有用。我每次发送时都会出现以下错误,但它对邮递员的效果很好。
Error: Access to XMLHttpRequest at 'http://127.0.0.1/mediacrow/mymail/mailer_php.php/' from origin 'http://127.0.0.1:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *',
Angular:contact.component.ts
send() {
console.log(this.contactForm.value);
this.http.post(`http://127.0.0.1/mediacrow/mymail/mailer_php.php/`,
{
data: this.contactForm.value,
headers:
{
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.subscribe((resp) => {
this.snackBar.open(
'Thank you for your submission!',
'', {duration: 4000}
);
}, (err) => {
this.snackBar.open(
'This was an error with your submission!',
'', {duration: 4000}
);
});
}
这是我的电子邮件脚本
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
//add more for creating a post
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-headers,
Content-Type, Access-Control-Allow-Methods');
$errors = '';
if( empty( $errors ) ) {
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$from_email = $request->email;
$message = $request->message;
$phone = $request->phone;
$platform = $request->platform;
$browser = $request->browser;
$name = $request->firstname.' '.$request->lastname;
$from_name = $name;
$response_array = array();
$to_email = "[email protected]";
$contact = "<p><strong>Name: </strong> $from_name</p><p><strong>Email:</strong> $from_email</p>";
$content = "<p><strong>Phone Number: </strong>$phone</p><p><strong>Browser: </strong>$browser</p>
<p><strong>Platform: </strong>$platform</p><p><strong>Message: </strong>$message</p>";
$website = "M&M";
$email_subject = "Contact Form";
$email_body = "<html><body>";
$email_body .= "$contact $content";
$email_body .= "</body></html>";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $from_email\n";
$headers .= "Reply-To: $from_email";
mail( $to_email, $email_subject, $email_body, $headers );
$response_array['status'] = 'success';
$response_array['from'] = $from_email;
echo json_encode( $response_array );
} else {
$response_array['status'] = 'error';
echo json_encode($response_array);
}
?>
我会非常感激,因为我不知道会出现什么问题,因为它在邮递员身上很有效
OPTION
请求在您的POST
请求发送之前发送。但是在你的PHP代码中,你不允许使用OPTION
方法。
这不是PHP代码,但我认为this link(CORS JSON php No 'Access-Control-Allow-Origin' header is present on the requested resource)对您有所帮助。