跳到主要内容

跨域资源共享 CORS

1 分钟阅读

概述

通过配置合适的响应头,可以明确指定允许的来源域、请求方法和头部信息。

Node.js 配置

app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
res.header("Access-Control-Allow-Credentials", "true");
next();
});

Nginx 配置

server {
listen 80;
server_name localhost; #你的域名
root html;
index index.html index.htm;

add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Credentials "true";
add_header Access-Control-Allow-Methods "PUT,POST,GET,DELETE,OPTIONS";
add_header Access-Control-Allow-Headers "token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,XRequested-With";

location / {
proxy_pass http://localhost:3000; #你的代理服务
}
}
评论
0条评论

添加新评论

昵称
邮箱
网址