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

前端解决跨域问题的8种方案 #4

Open
Jogiter opened this issue Jun 8, 2018 · 0 comments
Open

前端解决跨域问题的8种方案 #4

Jogiter opened this issue Jun 8, 2018 · 0 comments
Labels

Comments

@Jogiter
Copy link
Owner

Jogiter commented Jun 8, 2018

前端解决跨域问题的8种方案

  • document.domain + iframe
  • 动态创建script
  • location.hash + iframe
  • window.name + iframe
  • postMessage(HTML5中的XMLHttpRequest Level 2中的API)
  • CORS
  • JSONP
  • web sockets
  1. axios 请求时如何携带 cookie 信息?

前端

// 全局配置
axios.defaults.withCredentials = true 

// 请求中设置 https://github.com/axios/axios#user-content-request-config
{
  url: '/user',
  withCredentials: true, // default false
  method: 'get', // default
  baseURL: 'https://some-domain.com/api/',
}

后端

// 设置 header
"Access-Control-Allow-Credentials", "true"
"Access-Control-Allow-Origin", "https://api.jogiter.com"

因为 cookie 是针对域名的,所以需要指明具体域名,如果有多个域名可以用 ‘,’ 分隔

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

No branches or pull requests

1 participant