VUE AXIOS 跨域問題


背景:

   后台跨域使用通配符:context.Response.Headers.Add("Access-Control-Allow-Origin", "*");

  前端使用VUE axios 請求:

getData() {
this.$axios.get('http://127.0.0.1:8080/xxx/xxxxx',{
headers: {
"Content-Type":"application/json;charset=utf-8"
}
}).then( (response) => {
if( response.data.code === '0000'){
this.items = response.data.data.xxx;
console.log(this.items)
}
})
}
View Code

  

調試:

  報錯: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'
 

  添加  credentials: 'omit',

getData() {
        this.$axios.get('http://127.0.0.1:8080/xxx/xxxxx',{
          headers: {
            "Content-Type":"application/json;charset=utf-8"
          },
          credentials: 'omit',
        }).then( (response) => {
          if( response.data.code === '0000'){
            this.items = response.data.data.xxx;
            console.log(this.items)
          }
        })
      }
View Code

 

釋義:https://developer.mozilla.org/zh-CN/docs/Web/API/Request/credentials

credentials 是Request接口的只讀屬性,用於表示用戶代理是否應該在跨域請求的情況下從其他域發送cookies。這與XHR的withCredentials 標志相似,不同的是有三個可選值(后者是兩個):

  • omit: 從不發送cookies.
  • same-origin: 只有當URL與響應腳本同源才發送 cookies、 HTTP Basic authentication 等驗證信息.(瀏覽器默認值,在舊版本瀏覽器,例如safari 11依舊是omit,safari 12已更改)
  • include: 不論是不是跨域的請求,總是發送請求資源域在本地的 cookies、 HTTP Basic authentication 等驗證信息.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM