jQuery 如何在 $.ajax() 中参数化数组?
我正在使用一个插件来执行非 Ajax 帖子。但是,我的数组只是用“[Object = object]”提交的,所以我显然需要对此数组对象进行一些参数化。它并不像将其放入哈希中那么简单:
{ 'myarray': myarray }
当您将上述内容发送到 $.ajax() 时,它会将其更改为类似以下内容:
{ 'myarray': {'1': arrayfirsthash, '2': arraysecondhash, ...., '3': arraynhash } }
必须有一些 jQuery 函数可以执行该转换。它是什么?我可以公开称呼它吗?
感谢您提供的任何帮助。
更新:为了澄清我的问题,我实际上想看看如何将该值放置在表单元素中,以便服务器将其理解为哈希值。即使我将 JSON 编码的哈希值放入表单元素中,它仍然会被服务器显示为“对象”。我需要以某种方式序列化这个 JSON 哈希,以便可以将其发布到我的服务器。
I'm using a plugin to do a non-Ajax post. However, my array is just submitted with "[Object = object]", so I clearly need to do some parameterization on this array object. It's not as simple as putting it in a hash like:
{ 'myarray': myarray }
When you send the above to $.ajax(), it changes it to something like:
{ 'myarray': {'1': arrayfirsthash, '2': arraysecondhash, ...., '3': arraynhash } }
There must be some jQuery function that does that conversion. What is it, and can I call it publicly?
Thanks for any help you can offer.
UPDATE: To clarify my question, I'm actually looking to see how I can place this value in a form element so that it will be understood as a hash by the server. Even if I place the JSON-encoded hash in a form element, it still comes out as "Object" by the server. I need to somehow serialize this JSON hash so that it can be POSTED to my server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
参数的格式取决于您执行的是 GET 还是 POST 操作。
如果您执行 POST,则参数将在消息正文中发送。
如果服务需要 JSON 格式的信息,则必须在将数据传递到 .ajax 调用(作为“data”参数)之前显式序列化数据。
可以这样序列化:
JSON.stringify
函数仅受更高版本的浏览器支持。我相信您可以下载一些 javscript 插件来“spak-fill”此功能 - 为不支持它的浏览器启用此功能。请参阅此问题。
If you are performing a web GET then the parameters are encoded into the query string.
.ajax 调用将自动执行此编码,但如果您想明确执行此操作,jQuery 会提供 .param 函数。
The format of the parameters will depend upon whether you are performing a GET or a POST operation.
If you are performing a POST then the parameters are sent within the message body.
If the service is expecting the information in JSON, you will have to explicitly serialize the data before passing it into your .ajax call (as the 'data' parameter).
It can be serialized this way:
The
JSON.stringify
function is only supported by later browsers.I believe there are some javscript plugins you can download which "spak-fill" this functionality - enabling this for browsers that don't support it. See this question.
If you are performing a web GET then the parameters are encoded into the query string.
The .ajax call will perform this encoding automatically, but it you wanted to do this explicitly jQuery provides the .param function.
我相信您想从对象创建参数字符串。为此,您可以使用
$.param()
: http://api .jquery.com/jquery.param/这是在对象上使用
$.param()
的 jsfiddle:http://jsfiddle.net/jasper/eVLVJ/注意:上面的代码是
$.param()
文档页面上的示例。I believe you want create a parameter string from an object. To do this you can use
$.param()
: http://api.jquery.com/jquery.param/Here is a jsfiddle of using
$.param()
on an object: http://jsfiddle.net/jasper/eVLVJ/Note: the above code is an example on the documentation page for
$.param()
.