ajax中參數traditional的作用
阿新 • • 發佈:2019-04-06
type 字段 velt container nbsp body ber 測試 ace
在使用ajax向後臺傳值的時候,有的時候一個字段需要傳多個值,這種情況下會想到用數組形式來傳,比如:
1 2 3 4 5 6 7 8 9 10 |
$.ajax({
type: "post" ,
async: true ,
data: {
"records" : [ "123" , "456" , "789" ]
},
url: "xxxxx" ,
error: function (request) {},
success: function (data) {}
});
|
但是通過測試很快就會發現java後臺無法取到參數,因為jQuery需要調用jQuery.param序列化參數,jQuery.param(obj, traditional )默認情況下traditional為false,即jquery會深度序列化參數對象,以適應如PHP和Ruby on Rails框架,但servelt api無法處理,我們可以通過設置traditional 為true阻止深度序列化,然後序列化結果如下:
records: ["123", "456", "789"] => records=123&p=456&p=789
隨即,我們就可以在後臺通過request.getParameterValues()來獲取參數的值數組了,如下:
1 2 3 4 5 6 7 8 9 10 11 |
$.ajax({
type: "post" ,
async: true ,
traditional: true ,
data: {
"records" : [ "123" , "456" , "789" ]
},
url: "xxxxx" ,
error: function (request) {},
success: function (data) {}
});
|
ajax中參數traditional的作用