AJAX发布jQuery更改数组参数的名称
我正在使用jQuery做一个简单的AJAXpost,效果很好:
var parameters = {firstName: 'John', lastName: 'Smith'}; $.post('http://api.example.com/rest', parameters, function(data) { alert('Response: ' + data.someResult); });
但是,当我向参数添加数组时,如下所示:
var parameters = {firstName: 'John', lastName: 'Smith', children: ['Susy', 'Billy']};
然后问题是当POST到服务器时, children
被更改为children[]
的参数名称(实际上URL编码为children%5B%5D
)。 我无法更改服务器以查找名称为children[]
参数,那么我该怎么办? 如何使用名称children
多个值? 为什么jQuery会更改我的参数名称?
我相信你需要启用traditional
参数编码。
请参阅http://api.jquery.com/jQuery.ajax/和http://api.jquery.com/jQuery.param
由于$.post
没有特定的选项,你需要恢复到$.ajax
或使用全局设置jQuery.ajaxSettings.traditional = true
。