3 回答

TA貢獻1842條經(jīng)驗 獲得超13個贊
Jquery.ajax不會像對GET數(shù)據(jù)那樣自動為您編碼POST數(shù)據(jù)。jQuery希望您的數(shù)據(jù)經(jīng)過預格式化,以附加到請求正文中,以直接通過網(wǎng)絡發(fā)送。
一種解決方案是使用jQuery.param函數(shù)來構(gòu)建查詢字符串,大多數(shù)處理POST請求的腳本都希望使用該字符串。
$.ajax({
url: 'superman',
type: 'POST',
data: jQuery.param({ field1: "hello", field2 : "hello2"}) ,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});
在這種情況下,該param方法會將數(shù)據(jù)格式化為:
field1=hello&field2=hello2
該Jquery.ajax文檔中說,有一個叫標志processData控制該編碼是否是自動還是沒有這樣做。該文檔說它默認為true,但這不是我在POST使用時觀察到的行為。
- 3 回答
- 0 關(guān)注
- 7330 瀏覽
添加回答
舉報