为什么会出现跨域问题?
- 跨域是浏览器的同源策略造成的。
- 不同网段下会出现跨域问题。
- 不同域名会出现跨域问题
- 不同协议会出现跨域问题
跨域怎么解决?
1. JSONP
var url = 'localhost:1111';
$.ajax ({
url: url,
dataType: 'jsonp',
type: "GET",
jsonpCallback: "test",
success: function (data) {
alert(data);
},
});
2. nginx
server {
listen 5222; // 端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { //配置首页
root html;
index index1.html index.htm;
}
location /xiaoyang/ { //相对路径
#root /xiaoyang/index.html;
alias /xiaoyang/;
index she-of-flower.html;
}
#error_page 404 /404.html; //错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3.node(core)
- 配置前端与node之间的跨域问题
- index.js 主文件配置core
const cors = require('cors');
app.use(cors());