鍍金池/ 問答/PHP/ axios自定義header參數(shù)跨域請(qǐng)求,php后臺(tái)怎么接收參數(shù)?

axios自定義header參數(shù)跨域請(qǐng)求,php后臺(tái)怎么接收參數(shù)?

service.interceptors.request.use(config => {
    //自定義參數(shù)
    config.headers['X-Token'] = getToken() 

  return config
}, error => {

})

clipboard.png

php后臺(tái)設(shè)置header

        header('Access-Control-Allow-Credentials:true');
        header('Access-Control-Allow-Origin:'.$_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept');
        header('Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS');

求問怎么解決?謝謝!

回答
編輯回答
心上人

axios的話,前端需要允許跨域,axios.defaults.withCredentials = true
后端加入header對(duì)應(yīng)的header

header("Access-Control-Allow-Credentials", "true"); 
header("Access-Control-Allow-Origin", "允許跨域的地址,如果全部允許寫*"); 

2018年3月8日 11:16
編輯回答
兔寶寶
header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept,X-Token');

加入 'X-Token' 參數(shù)就行了

2017年10月2日 20:58