如何使用jQuery和.ajax方法发送数组而不逃脱括号? [英] How do I send an array using jQuery and the .ajax method without escaping the brackets?

查看:189
本文介绍了如何使用jQuery和.ajax方法发送数组而不逃脱括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送具有多个值的相同参数名称,但是即使在阅读了SO上的帖子后也无法弄清楚该怎么做...我想拥有一个destination_input数组:

I am trying to send the same parameter name with multiple values, but even after reading the posts on SO can't figure out how to do it... I want to have an array of destination_input:

var myObject = {
                  search_zip: params.search_zip,
                  search_distance: params.search_distance,
                  filter_opts: params.filter_opts,
                  page: params.page,
                  destination_input: ['323 w concord pl #8, chicago, il', '11 e hubbard, chicago, il']
            };

但这会创建以下查询字符串:

but this creates a query string of:

search_zip=60614&search_distance=1&filter_opts=discount_check%2Cneed_device_check%2Cauto_track_check&destination_input%5B%5D=323+w+concord+pl+%238%2C+chicago%2C+il&destination_input%5B%5D=11+e+hubbard%2C+chicago%2C+il

如您所见,它不断将%5B%5D添加到destination_input,例如&destination_input%5B%5D=11+e+hubbard%2C+chicago%2C+il

As you can see it keeps adding %5B%5D to the destination_input such as &destination_input%5B%5D=11+e+hubbard%2C+chicago%2C+il

这意味着所有内容都在后端弄乱了.有什么想法吗?

This means everything gets messed up on the backend. Any ideas?

推荐答案

我假设您的后端不是PHP,因为PHP期望[]表示GET参数是多值的.这意味着您的后端将使用与数组元素相同的键来对待多个GET参数,对吗?

I'm going under the assumption that your backend is not PHP, since PHP expects [] to indicate that an GET parameter is multivalued. That means that your backend will treat multiple GET arguments with the same key as elements of an array, correct?

根据这个假设,您只需要告诉jQuery在将数组变成GET参数字符串时不要自动添加[].

Going off that assumption, you just need to tell jQuery to not automatically add [] when it is turning an array into a GET argument string.

为此,必须将'traditional': true参数作为选项传递给jQuery的ajax函数.

To so that, you have to pass the 'traditional': true argument as an option to jQuery's ajax function.

在此处搜索传统": http://api.jquery.com/jQuery.ajax/

这篇关于如何使用jQuery和.ajax方法发送数组而不逃脱括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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