在默認(rèn)情況下笤妙,對(duì)于PUT/POST/PATCH/DELETE等請(qǐng)求又碌,請(qǐng)求頭中的Content-Type為application/json讶迁,即JSON類型。有時(shí)我們需要將數(shù)據(jù)提交為指定類型礁鲁,如application/x-www-form-urlencoded奄毡、multipart/form-data牲阁、text/plain等泻红。下面以POST請(qǐng)求為例來(lái)說(shuō)明。
1.全局headers配置
Vue.http.headers.post['Content-Type']='application/x-www-form-urlencoded'
2.實(shí)例配置
this.$http.post(
'http://example.com/books',
function(data,status,request){
if(status==200){
console.dir(data)
}
},
{
//配置請(qǐng)求頭
headers:{
'Content-Type':'multipart/form-data'
}
}
)
注:實(shí)例配置的優(yōu)先級(jí)高于全局配置或颊,因此最終Content-Type為multipart/form-data砸紊。