这次给大家带来vue+php处理跨域问题,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'.
解决办法
文章链接:cors: credentials mode is ‘include'
xhrfields: { withcredentials: false},
把 withcredentials: true 改成 withcredentials: false,如果你没加上面那段代码当然也不会报这个错。虽然是解决方法很简单,但经此发现许多知识没掌握不得不梳理下。
•http 请求方式有许多种,有些请求会触发 cors 预检请求。“需预检的请求”会使用 options 方法发起一个预检请求到服务器,以获知服务器是否允许该实际请求。
•对于跨域请求浏览器一般不会发送身份凭证信息。如果要发送凭证信息,需要设置 xmlhttprequest 的 withcredentials 属性为 true:withcredentials: true。此时要求服务器的响应信息中携带 access-control-allow-credentials: true,否则响应内容将不会返回。
•对于携带身份凭证的请求,服务器不得设置 access-control-allow-origin 的值为“*”。因为请求头携带了 cookie 信息。要将 access-control-allow-origin 的值设置为 http://www.zrt.local:8080。
•另外,响应头中也携带了 set-cookie 字段,尝试对 cookie 进行修改。如果操作失败,将会抛出异常。
跨域请求想要带上 cookies 必须在请求头里面加上:
crossdomain: true, xhrfields: { withcredentials: true}
又变成文章开头的问题了,解决办法:
后台代码:
access-control-allow-origin: 'http://www.zrt.local:8080'access-control-allow-credentials: true
前端代码:
crossdomain: true, xhrfields: { withcredentials: true}
跟之前一样就行了。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
小程序开发分享页面后返回首页
vue的ui组件库自定义动态组件
以上就是vue+php处理跨域问题的详细内容。