[英]JQuery array of objects in ajax
I want an array of objects to be sent via post through ajax. 我想通过ajax通过post发送对象数组。 Background story: a user selects a number of rooms, and those rooms should be booked in another file using php.
背景故事:用户选择了多个房间,这些房间应该使用php预订在另一个文件中。
The array/objects is defined as: 数组/对象定义为:
var rooms = {};
rooms[rname] =
{
"rname": rname, // eg 1.27
"date": date, // eg 2013-04-24
"time": time, // eg 20:15
"duration": duration // eg 4
}
And the ajax call is made 然后进行ajax调用
$.ajax({
type: 'GET', //used get to see the parameters passed
url: 'book_room.php',
data: rooms
});
However, it seems that the request sent is somewhat like this: 但是,似乎发送的请求有点像这样:
http://url.dk/book_room.php?1.29%5Brname%5D=1.29&1.29%5Bdate%5D=2013-04-28&1.29%5Btime%5D=20%3A15&1.29%5Bduration%5D=4&2.25%5Brname%5D=2.25&2.25%5Bdate%5D=2013-04-28&2.25%5Btime%5D=20%3A15&2.25%5Bduration%5D=4
http://url.dk/book_room.php?1.29%5Brname%5D=1.29&1.29%5Bdate%5D=2013-04-28&1.29%5Btime%5D=20%3A15&1.29%5Bduration%5D=4&2 0.25%5Brname%5D = 2.25&2.25%5Bdate%5D = 2013年4月28日及2.25%5Btime%5D = 20%3A15&2.25%5Bduration%5D = 4
How can I successfully pass a set of rooms in a request so I can work with them properly? 如何在请求中成功传递一组房间,以便可以正确使用它们? I have a feeling that I have misunderstood the purpose of something here.
我感到自己误解了这里某些东西的目的。
Accorting to the API http://api.jquery.com/jQuery.ajax/ You should POST like: 根据API http://api.jquery.com/jQuery.ajax/进行配置,您应该像这样进行POST:
$.ajax({
contentType:"application/json",
url:yourEndPoint,
data:JSON.stringify(yourStuffGoesHere),
dataType:"json",
processData:false,
});
As adviced I will post the fix for the problem as an answer. 根据建议,我将发布该问题的修复程序作为答案。
I Found a solution where X requests are made based on how many rooms were chosen, and it works just fine. 我找到了一个解决方案,其中根据选择的房间数提出了X个请求,并且效果很好。
$.each(rooms, function(k,v) {
$.ajax({
type: 'GET',
url: 'book_room.php',
data: v
});
});
The requests made this time looks easy to work with. 这些请求使这次看起来很容易处理。 Example requests:
请求示例:
http://url.dk/book_room.php?rname=1.47&date=2013-04-28&time=21%3A00&duration=4 http://url.dk/book_room.php?rname=22.3&date=2013-04-28&time=21%3A00&duration=4
http://url.dk/book_room.php?rname=1.47&date=2013-04-28&time=21%3A00&duration=4 http://url.dk/book_room.php?rname=22.3&date=2013-04-28&time = 21%3A00&持续时间= 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.