鍍金池/ 問答/HTML/ 如何在axios中提交token,為何總報錯

如何在axios中提交token,為何總報錯

瀏覽器報錯:Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. The response had HTTP status code 403.

代碼:

   this.$axios({
        method: "get",
        url: "http://192.168.3.10:8080/MayaCloud_Login/getPermissionTree",
        headers:{
          sessionUser :sessionStorage.getItem("token")
        },
      })
回答
編輯回答
命多硬

報錯隨便一搜也知道是跨域了吧。

2017年10月25日 07:06
編輯回答
別逞強

是因為你為你的請求頭部添加了一個sesstionUser,并不在簡單請求的范圍內(nèi),所以瀏覽器會發(fā)送一個預(yù)檢請求
詳細簡單請求和復雜請求區(qū)別看這里
https://developer.mozilla.org...

解決辦法很簡單,后臺為你設(shè)置的頭添加跨域允許就可以了,要在對預(yù)檢請求的處理里添加

("Access-Control-Allow-Headers", "sessionUser");
2018年8月11日 03:04