1. 程式人生 > >vue-resource post請求,header無法設定解決方案

vue-resource post請求,header無法設定解決方案

在vue-resource 裡面加header頭的時候,get請求正常加入,但post,沒有正常加上,這種情況是因為options.emulateHTTP覆蓋導致的,在請求攔截器裡面加請求頭也不行,所以解決方案是全域性寫死請求頭:

Vue.http.options.emulateHTTP = true;
Vue.http.options.headers = {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}

這樣寫入之後會導致,BODY引數錯誤,所以需要在全域性請求攔截器裡面重新解析BODY資料

let body = request.body
if (body) { let array = [] for (var key in body) { array.push(key + '=' + body[key]) } request.body = array.join('&') }

這樣所有引數都會正常,header也就加上了。