没有括号乱七八糟的JQuery参数序列化 [英] JQuery parameter serialization without the bracket mess

查看:85
本文介绍了没有括号乱七八糟的JQuery参数序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JQuery向服务器发出JSON请求,并且它的参数序列化似乎是硬编码到PHP期望的,而不是通用的。基本上我有一个看起来像这样的对象:

I'm using JQuery to make a JSON request back to the server and it seems that it's parameter serialization is hard-coded to what PHP expects instead of being generic in nature. Basically I have an object that looks like this:

{
    foo: 1,
    bar : [1, 3, 5]
}

并将其序列化为:

foo=1&bar[]=1&bar[]=3&bar[]=5

无论如何都可以做到吗?

Is there anyway to make it just do?

foo=1&bar=1&bar=3&bar=5

似乎我认为jQuery不应该与少数服务器端框架所期望的命名约定相关联。如果我希望我的param被称为 bar [] 我可以轻松地将其命名为自己,如果它是我的服务器端代码所期望的那样。

It seems to me that jQuery shouldn't be so tied to what a handful of server side frameworks expect as a naming convention. If I wanted my param to be called bar[] I could easily name it that myself if it's what my server-side code expects.

推荐答案

我假设你正在使用JQuery 1.4。你应该看看这个: http://benalman.com/ news / 2009/12 / jquery-14-param-demystified /

I'm assuming you're using JQuery 1.4. You should take a look at this: http://benalman.com/news/2009/12/jquery-14-param-demystified/

作者讨论了为什么他们让JQuery以这种方式表现,并提出了一些优点。例如,如果您不使用方括号语法,则不能仅使用单个值传入数组。

The author discusses why they made JQuery behave this way, and makes some excellent points. For example, if you don't use the "square bracket syntax", you can't pass in arrays with only a single value.

他还提供了一项工作 - 周围:

He also offers a work-around:

$.ajaxSetup({ traditional: true });

这将告诉JQuery使用非括号的序列化方法。

This will tell JQuery to use the non-bracket method of serialization.

这篇关于没有括号乱七八糟的JQuery参数序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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