Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问一下CORS方案解决跨域问题该怎么处理呢? #3

Open
Craig-Cheng opened this issue Feb 25, 2018 · 2 comments
Open

Comments

@Craig-Cheng
Copy link

No description provided.

@xxxgitone
Copy link
Owner

xxxgitone commented Feb 25, 2018

CORS处理需要后台配合,后台设置几个响应头信息就可以了,以express为例

app.get('/', (req, res) => {
  res.set('Access-Control-Allow-Origin', 'http://example.com'); // 允许的origin
  res.set('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH'); // 允许的HTTP方法
  res.set('Access-Control-Allow-Headers', 'Content-Type'); // 支持的头信息字段,可以设置多种
  res.set('Access-Control-Allow-Credentials', true); // 可选,表示是否允许发送Cookie
  res.send('hello')
})

也可以写成一个中间件,放在你要跨域的路由之前即可.

当然使用了nginx的话,可以配置nginx,原理还是一样,也是设置这些头信息.你可以去查查相关资料哈!

@Craig-Cheng

@Craig-Cheng
Copy link
Author

非常感谢, 我试试看

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants