hello world~
以前什么都不懂,写个前端分离的项目,总遇到各种问题需要百度。
如今项目做多了。。
终于学会了谷歌。
// 自定义header
axios.interceptors.request.use(function (config) {
let token = getCookie(TOKEN_KEY)
if (token) {
config.headers.login_token = token
}
return config
})
解决
问题:跨域请求。
为什么会发送OPTIONS请求?
其实跨域分为 简单跨域请求 和 复杂跨域请求
简单跨域请求是不会发送OPTIONS
请求的
复杂跨域请求才会发送一个预检请求 OPTIONS
复杂跨域请求要满足以下:
1. 请求方式不是GET、POST、HEAD
2. POST请求的Content-Type
并非 application/x-www-form-urlencoded, multipart/form-data,
或 text/plain
3. 请求设置了自定义header字段
如果不想发送option
请求可以改为简单请求 比如你的Content-Type
可能是application/json
格式 将其改为application/x-www-form-urlencoded