首页 > 解决方案 > CORS 策略已阻止对来自源的请求的访问。电子邮件发送失败并出现此错误

问题描述

我无法从我的网站(使用HTML, CSS,开发JavaScript)发送电子邮件。

我在发送电子邮件时收到以下错误消息:

从源“ http://www.mobilfiksern.no ”访问“ http://mobilfiksern.no/contactform.php ”的 XMLHttpRequest已被 CORS 策略阻止:不存在“Access-Control-Allow-Origin”标头在请求的资源上。

有人可以帮我解决这个问题吗?

标签: javascripthtmlcsscors

解决方案


每个页面顶部或公共顶部 php 文件请添加以下行。

<?php
//Session start section
?>    
<?php

//Need to add the following codes
$origin = $_SERVER['HTTP_ORIGIN'];
// PHP 7.x version array
$allowed_domains = ['http://mobilfiksern.no','http://www.mobilfiksern.no'];
// PHP 5.x version array
//$allowed_domains = array("http://mobilfiksern.no", "http://www.mobilfiksern.no");

if (in_array($origin, $allowed_domains)) {
    header('Access-Control-Allow-Origin: ' . $origin);
}
?>
<?php
//Usual codes
?>
<?php //Start the html tags ?>
<html>

推荐阅读