首页 技术 正文
技术 2022年11月21日
0 收藏 515 点赞 3,505 浏览 910 个字

跨站访问

为什么浏览器禁止跨域访问

Nginx跨站访问

Syntax:add_header name value [always];

Default:——

Context:http,server,location,if in location

Access-Control-Allow-Origin     # 通过header判断是否可以跨站访问

演示

创建一个html文件

vi /opt/app/code/test_oringin.html

<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://www.joy.com/1.html", #如果访问跨域成功,返回success
success: function(data) {
alert("sucess!!!");
},
error: function() {
alert("fail!!!,请刷新再试!"); # 如果访问跨域成功,返回“请刷新再试”
}
});
});
</script>
<body>
<h1>测试跨域访问</h1>
</body>
</html>

没有配置语法访问,会返回刷新

配置语法


location ~ .*\.(htm|html) {
#expires 24h;
#add_header Access-Control-Allow-Crigin http://www.joy.com; # 允许跨域访问的域名
#add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; # 配置请求方式
root /opt/app/code;
}

再次访问,可以成功跳转到http://www.joy.com/1.html

注意:配置语法中的域名,需要公网的域名

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290