Vue项⽬中遇到的跨域问题及解决⽅法(后台php)
问题描述
前端 vue 框架,后台 php,百度跨域问题后台加这段代码
header("Access-Control-Allow-Origin: *");
加了之后报这个错:
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the
request's credentials mode is 'include'.
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the
request's credentials mode is 'include'.
解决办法
⽂章链接:
xhrFields: {
withCredentials: false
},
把withCredentials: true改成withCredentials: false,如果你没加上⾯那段代码当然也不会报这个错。虽然是解决⽅法很简单,但经此发现许多知识没掌握不得不梳理下。
•HTTP 请求⽅式有许多种,有些请求会触发 CORS 预检请求。“需预检的请求”会使⽤ OPTIONS ⽅法发起⼀个预检请求到服务器,以获知服务器是否允许该实际请求。
•对于跨域请求浏览器⼀般不会发送⾝份凭证信息。如果要发送凭证信息,需要设置 XMLHttpRequest 的 withCredentials 属性为 true:withCredentials: true。此时要求服务器的响应信息中携带 Access-Control-Allow-Credentials: true,否则响应内容将不会返回。
•另外,响应头中也携带了 Set-Cookie 字段,尝试对 Cookie 进⾏修改。如果操作失败,将会抛出异常。
跨域请求想要带上 cookies 必须在请求头⾥⾯加上:
crossDomain: true,
xhrFields: {
withCredentials: true
}
⼜变成⽂章开头的问题了,解决办法:
后台代码:
Access-Control-Allow-Origin: 'local:8080'
Access-Control-Allow-Credentials: true
php中header是什么意思前端代码:
crossDomain: true,
xhrFields: {
withCredentials: true
}
跟之前⼀样就⾏了。
总结
以上所述是⼩编给⼤家介绍的Vue 项⽬中遇到的跨域问题及解决⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!