使用 jQuery,如何将字符串数组作为 http 参数附加到 http 请求?
我有一个 spring 控制器,其请求映射如下
@RequestMapping("/downloadSelected")
public void downloadSelected(@RequestParam String[] ids) {
// retrieve the file and write it to the http response outputstream
}
我有一个 html 对象表,其中每一行都有一个复选框,其中对象的 id 作为值。当他们提交时,我有一个 jQuery 回调来序列化所有 id。我想将这些 id 粘贴到名为“ids”的 http 请求参数中,以便我可以轻松获取它们。
我想我可以执行以下操作
var ids = $("#downloadall").serializeArray();
然后我需要获取每个 id 并将它们添加到名为 ids 的请求参数中。但有没有一种“标准”的方法可以做到这一点?喜欢使用 jQuery?
I have a spring controller with a request mapping as follows
@RequestMapping("/downloadSelected")
public void downloadSelected(@RequestParam String[] ids) {
// retrieve the file and write it to the http response outputstream
}
I have an html table of objects which for every row has a checkbox with the id of the object as the value. When they submit, I have a jQuery callback to serialize all ids. I want to stick those ids into an http request parameter called, "ids" so that I can grab them easily.
I figured I could do the following
var ids = $("#downloadall").serializeArray();
Then I would need to take each of the ids and add them to a request param called ids. But is there a "standard" way to do this? Like using jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道“标准方式”,但这就是我要做的。
将为您提供表单上的数据集(仅显示已检查的项目):
将其提供给 jQuery 的 .ajax() 只是:
Array.map() 并不与所有浏览器兼容,因此您最好在页面上也包含此代码:
此代码片段是我从 mozilla 开发人员中心获得的。
我没有将它们放在
?ids=...
参数中,但这样它们就可以在服务器端轻松访问。您始终可以修改地图功能来满足您的需求。I don't know about "standard way", but this is how I would do it.
will give you a dataset on the form (only the checked items presented):
To feed this to jQuery's .ajax() just:
The Array.map() is not compatible with all browsers yet so you better have this code on your page too:
This code snippet I got from mozilla developer center.
I didn't put them in a
?ids=...
param, but this way they are easy to access on server side. You can always just modify the map function to fit your needs.