Javascript 将一组对象作为 ajax 发布数据发送?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10261021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending an array of objects as ajax post data?
提问by Ben
My overall goal is to get all some of all drop-downs on a page and send that to be processed by a php file.
我的总体目标是获取页面上的所有下拉列表,并将其发送给 php 文件进行处理。
Right now, the way I'm doing it in jQuery is making an overall schedule array and then adding each element to be updated to that array. So I have something like:
现在,我在 jQuery 中的做法是制作一个整体计划数组,然后将要更新的每个元素添加到该数组中。所以我有类似的东西:
var schedule = [];
var data = {
'user_id' : '12',
'day_of_week' : 'Monday',
'when' : 'start',
'time' : '12 AM'
}
schedule.push(data);
var data = {
'user_id' : '13',
'day_of_week' : 'Tuesday',
'when' : 'end',
'time' : '12 AM'
}
schedule.push(data);
// schedule would have two objects in it
Obviously in loops and and stuff.
显然在循环和东西中。
So, my schedule array has two objects in it, in this case.
因此,在这种情况下,我的日程表数组中有两个对象。
Now, is it possible to use that schedule array as the ajax data? It doesn't work if I do something like:
现在,是否可以将该调度数组用作 ajax 数据?如果我执行以下操作,则不起作用:
$.ajax({
url: 'http://something.com/ajax',
data: schedule,
type: 'POST'
});
But if I instead change it to schedule[0]
it works just fine, but only for the first thing in the schedule array, obviously.
但是,如果我改为将其更改为schedule[0]
它可以正常工作,但显然仅适用于计划数组中的第一件事。
采纳答案by Chris Baker
Make sure you are using the correct version of jQuery. In earlier versions, you had to pass a sting; new versions use "intelligent guess" on the data
variable. You can either explicitly tell jQuery that you're passing it a javascript object with the dataType
parameter, or you can let jQuery figure it out.
确保您使用的是正确版本的 jQuery。在早期版本中,您必须通过一个刺痛;新版本对data
变量使用“智能猜测” 。您可以明确地告诉 jQuery 您正在向它传递一个带有dataType
参数的 javascript 对象,或者您可以让 jQuery 弄清楚。
Documentation
文档
jQuery.ajax()
- http://api.jquery.com/jQuery.ajax/
jQuery.ajax()
- http://api.jquery.com/jQuery.ajax/
回答by noob
The data
attribute should be an object.
的data
属性应该是一个对象。
What you can do is this:
你可以做的是:
$.ajax({
url: 'http://something.com/ajax',
data: {schedule: schedule},
type: 'POST'
});
So if you receive this for example in PHP you have $_POST["schedule"]
. An that is exactly the same as you had in JavaScript.
因此,如果您在 PHP 中收到此示例,则您有$_POST["schedule"]
. 这与您在 JavaScript 中的完全相同。
Ohh yes I've forgot... also have a look at .serialize()
and .serializeArray()
!
哦,是的,我忘了……也看看.serialize()
和.serializeArray()
!
回答by Madara's Ghost
Pass it as JSON:
将其作为 JSON 传递:
$.ajax({
url: 'http://something.com/ajax',
data: {schedule: schedule},
type: 'POST',
dataType: 'JSON'
});
It would send a JSON encoded string to the server, which server-side languages can parse. (in PHP, it's done with json_decode()
).
它会将 JSON 编码的字符串发送到服务器,服务器端语言可以解析该字符串。(在 PHP 中,它是用 完成的json_decode()
)。