解决跨域有多重,在这里主要讲用nginx解决跨域javascript
nginx下载地址html
前端代码:
利用jQuery的ajax api发送请求前端
<button id="getOK">发送请求OK(客户端解决跨域问题)</button> <button id="getNO">发送请求NO(客户端解决跨域问题)</button> <script> $(document).ready(function () { $('#getOK').click(function () { $.ajax({ url:'http://localhost:3000/ok', success:function(res) { console.log("success",res) }, error:function(err) { console.log('fail',err) } }) }) $('#getNO').click(function () { $.ajax({ url:'http://localhost:3000/no', success:function(res) { console.log("success",res) }, error:function(err) { console.log('fail',err) } }) }) }) </script>
后端代码:
利用node的express框架开启服务,并根据url返回json格式的数据,
设置这么多接口的目的是为了后面匹配nginx的location配置的java
const express = require('express') const cookieParser = require('cookie-parser') var app = express() var router = express.Router() router.get('/ok',function (req,res) { res.json({ code:200, msg:"isOK" }) }) router.get('/ok/son',function (req,res) { res.json({ code:200, msg:"isOKSon" }) }) router.get('/ok2',function (req,res) { res.json({ code:200, msg:"isOK2" }) }) router.get('/no',function (req,res) { res.json({ code:200, msg:"isNO" }) }) router.get('/no/son',function (req,res) { res.json({ code:200, msg:"isNOSON" }) }) router.get('/no/son2',function (req,res) { res.json({ code:200, msg:"isNOSON2" }) }) app.use(router) app.use(cookieParser) app.listen(3000,function () { console.log('listen in 3000') })
而后开启node服务
如今能够测试下接口
能够看出,node服务成功开启
如今能够尝试不开启nginx服务直接发送ajax请求会出现什么状况
(注意:发送ajax请求须要以服务器方式打开网页,不能以文件形式)
如图,在5500端口请求3000端口出现了跨域问题,这时候就能够开启nginx服务并配置location进行解决node
反向代理的原理就是讲前端的地址和后端的地址用nginx转发到同一个地址下,如5500端口和3000端口都转到3003端口下,具体配置以下:jquery
具体的location配置规则以下:
nginx的location配置规则nginx
server { listen 3003; server_name localhost; ## = /表示精确匹配路径为/的url,真实访问为http://localhost:5500 location = / { proxy_pass http://localhost:5500; } ## /no 表示以/no开头的url,包括/no1,no/son,或者no/son/grandson ## 真实访问为http://localhost:5500/no开头的url ## 若 proxy_pass最后为/ 如http://localhost:3000/;匹配/no/son,则真实匹配为http://localhost:3000/son location /no { proxy_pass http://localhost:3000; } ## /ok/表示精确匹配以ok开头的url,/ok2是匹配不到的,/ok/son则能够 location /ok/ { proxy_pass http://localhost:3000; } }
上面代码的意思是将localhost:3003转发为location:5500,也就是说如今访问localhost:3003其实是访问location:5500,而访问localhost:3003/no则是访问localhost:3000,并以no开头的urlgit
如今修改前端代码,将以前请求的接口的端口换为3003,以下:github
$('#getOK').click(function () { $.ajax({ url:'http://localhost:3003/ok', success:function(res) { console.log("success",res) }, error:function(err) { console.log('fail',err) } }) })
在浏览器访问的也不算location:5500,而是localhost:3003了,再次发送请求也不会出现跨域问题了,由于他们都是同一个域了,这就是nginx反向代理ajax
这是前端代码
$(document).ready(function () { $('#get').click(function () { $.ajax({ url:'http://localhost:3002/ok', // 带cookies的请求 xhrFields:{ withCredentials:true }, success:function(res) { console.log("success",res) }, error:function(err) { console.log('fail',err) } }) }) })
后端代码同前面
还有nginx配置以下:
server { listen 3002; server_name localhost; location /ok { proxy_pass http://localhost:3000; # 指定容许跨域的方法,*表明全部 add_header Access-Control-Allow-Methods *; # 预检命令的缓存,若是不缓存每次会发送两次请求 add_header Access-Control-Max-Age 3600; # 带cookie请求须要加上这个字段,并设置为true add_header Access-Control-Allow-Credentials true; # 表示容许这个域跨域调用(客户端发送请求的域名和端口) # $http_origin动态获取请求客户端请求的域 不用*的缘由是带cookie的请求不支持*号 add_header Access-Control-Allow-Origin $http_origin; # 表示请求头的字段 动态获取 add_header Access-Control-Allow-Headers $http_access_control_request_headers; # OPTIONS预检命令,预检命令经过时才发送请求 # 检查请求的类型是否是预检命令 if ($request_method = OPTIONS){ return 200; } } }
发送预检命令的是非简单请求,具体能够看慕课网ajax跨域彻底讲解
实际上不是非简单请求的且不带cookie只需2个字段便可解决跨域
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Origin $http_origin;
这时只需改ajax请求的端口接口,无需修改前端服务器的地址
最后附上源码:
nginx解决跨域问题