我正在使用 jQuery 做一个简单的 AJAX 帖子,效果很好:
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']};
那么问题是参数名称 children
更改为 children[]
(它实际上是 URL 编码为 children%5B%5D
)当发布到服务器时。我无法更改服务器来查找名称为 children[]
的参数,那么我该怎么办?如何发布名为 children
的多个值?为什么 jQuery 改变我的参数名称?
最佳答案
我相信您需要启用传统
参数编码。
参见http://api.jquery.com/jQuery.ajax/和 http://api.jquery.com/jQuery.param
由于 $.post
没有为此提供特定选项,您需要恢复到 $.ajax
或使用全局设置 jQuery .ajaxSettings.traditional = true
.
关于AJAX post 使用 jQuery 更改数组参数的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5888030/