vue php跨域,Vue+php处理跨域问题
這次給大家帶來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
}
跟之前一樣就行了。
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
總結
以上是生活随笔為你收集整理的vue php跨域,Vue+php处理跨域问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经典卷积神经网络---VGG16详解
- 下一篇: 安拆网:脚手架钢管规格及施工要求