javascript - CORS 策略已阻止对来自源的请求的访问。电子邮件发送失败并出现此错误
问题描述
我无法从我的网站(使用HTML
, CSS
,开发JavaScript
)发送电子邮件。
我在发送电子邮件时收到以下错误消息:
从源“ http://www.mobilfiksern.no ”访问“ http://mobilfiksern.no/contactform.php ”的 XMLHttpRequest已被 CORS 策略阻止:不存在“Access-Control-Allow-Origin”标头在请求的资源上。
有人可以帮我解决这个问题吗?
解决方案
每个页面顶部或公共顶部 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>
推荐阅读
- python - PCA - 取平均值的差异
- php - 无法使用其他表单中的 php 将数据插入 mysql 数据库
- c - 从静态 C 库的源代码中提取函数定义以避免库编译
- javascript - 如何使用 Vue JS 在子组件中保留数字和增量?
- computer-vision - YOLOv3 错误的边界框预测
- c# - 如何从报告中获取最大时间
- c++ - 为什么我的复制构造函数不起作用?(C++)
- ruby-on-rails - 如何使用闭包树对嵌套哈希树(注释)进行分页并将分页
- ios - Xcode 在添加 swift 包时提示存储库被锁定
- c# - 我的代码在 dataGridView1.Rows.Clear() 之后直到结束都不起作用