首页 技术 正文
技术 2022年11月21日
0 收藏 558 点赞 3,602 浏览 1215 个字

1.定义

跨域是指a页面想获取b页面资源,如果a、b页面的协议、域名、端口、子域名不同,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制了跨域访问,也就是不允许跨域请求资源。注意:跨域限制访问,其实是浏览器的限制。理解这一点很重要!!!

2.跨域访问示例

假设有两个网站,A网站部署在:http://localhost:7999 即本地ip端口上;B网站部署在:http://localhost:8112 即本地ip端口82上。

现在A网站的页面想去访问B网站的信息,A网站页面的代码如下(这里使用jquery的异步请求):

修改前的js代码:在服务器:localhost:7999服务,访问localhost:8112端口的资源

$(function () {
$.ajax({
method:'GET',
url:'http://localhost:8112/visual/test/?a=1&b=2',
success:function (arg) {
alert(arg)
}
})
});

修改后的ajax请求:

$(function () {
$.ajax({
method:'GET',
url:'/proxy/visual/test/?a=1&b=2',
success:function (arg) {
alert(arg)
}
})
});

nginx的最新配置:

server {
listen ;
server_name localhost; charset utf-; access_log /val/log/test/.log access; location / {
root //usr/local/server/test_service/public;
index index.html index.htm index.php; if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$ last;
break;
}
} #error_page /.html; error_page /50x.html;
location = /50x.html {
root html;
} location ~ \.php$ {
root /usr/local/server/test_service/public;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} location ^~/proxy/{ // 这是需要添加的配置 proxy和上面ajax开头的url一致, 表示拦截/proxy的请求
rewrite ^/proxy/(.*)$ /$ break; 表示匹配到后就暂停匹配
proxy_pass http://localhost:8112/; 表示将匹配到的转发到你需要访问的服务的主机和端口
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902