jQuery如何在$ .ajax()中参数化数组? [英] How does jQuery parameterize an array in $.ajax()?

查看:92
本文介绍了jQuery如何在$ .ajax()中参数化数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用插件发布非Ajax帖子.但是,我的数组只是使用"[Object = object]"提交的,因此我显然需要对该数组对象进行一些参数化.这并不像将其放入哈希这样简单:

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 }

将以上内容发送到$ .ajax()时,它将更改为:

When you send the above to $.ajax(), it changes it to something like:

{ 'myarray': {'1': arrayfirsthash, '2': arraysecondhash, ...., '3': arraynhash } }

必须有一些执行该转换的jQuery函数.这是什么,我可以公开地称呼它吗?

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.

更新:为澄清我的问题,我实际上是在寻找如何将该值放置在表单元素中,以便服务器将其理解为哈希值.即使我将JSON编码的哈希放在表单元素中,服务器仍然会将其显示为对象".我需要以某种方式序列化此JSON哈希,以便可以将其发布到我的服务器上.

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.

推荐答案

参数的格式取决于您执行的是GET还是POST操作.

The format of the parameters will depend upon whether you are performing a GET or a POST operation.

如果要执行POST,则将在消息正文中发送参数.

If you are performing a POST then the parameters are sent within the message body.

如果服务期望以JSON格式提供信息,则必须先显式序列化数据,然后再将其传递到.ajax调用中(作为"data"参数).

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:

    contentSerialized = JSON.stringify(content);

JSON.stringify功能仅受更高版本的浏览器支持.

The JSON.stringify function is only supported by later browsers.

我相信您可以下载一些javscript插件来填充"此功能-在不支持该功能的浏览器中启用该功能.参见此问题.

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.


如果您正在执行Web GET,则参数将被编码到查询字符串中.


If you are performing a web GET then the parameters are encoded into the query string.

.ajax调用将自动执行此编码,但是您要明确地执行此编码,jQuery提供了 .param 函数.

The .ajax call will perform this encoding automatically, but it you wanted to do this explicitly jQuery provides the .param function.

这篇关于jQuery如何在$ .ajax()中参数化数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆