1. 程式人生 > >AJAX請求頭Content-type

AJAX請求頭Content-type

setRequestHeader 設定請求頭

設定請求頭前需先呼叫open方法開啟一個url

xhr.open("post", "/save");

設定資料格式

傳送json格式資料

xhr.setRequestHeader("Content-type","application/json; charset=utf-8");

傳送表單資料

xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

傳送純文字

不指定Content-type時,此是預設值值

xhr.setRequestHeader("Content-type", "text/plain; charset=utf-8");

傳送html文字

xhr.setRequestHeader("Content-type", "text/html; charset=utf-8");

編碼可帶可不帶

// 不帶字元編碼寫法
xhr.setRequestHeader("Content-type", "application/json");

值對大小寫不敏感

xhr.setRequestHeader("Content-type","Application/JSON; charset=utf-8"
);

雖然如此,但還是都保持小寫