如何在jQuery的ajax()调用中传递多个JavaScript数据变量?

12

如果startDateTimeendDateTime是类似于这样的dateTime值:

Start: Mon Jan 10 2011 18:15:00 GMT+0000 (GMT Standard Time)
End: Mon Jan 10 2011 18:45:00 GMT+0000 (GMT Standard Time)

如何将 startDateTimeendDateTime 一起传递到下面的 ajax 调用中?

eventNew : function(calEvent, event) 
{
    var startDateTime = calEvent.start;
    var endDateTime = calEvent.end;
    jQuery.ajax(
    {
        url: '/eventnew/',
        cache: false,
        data: /** How to pass startDateTime & endDateTime here? */,
        type: 'POST',
        success: function(response)
        {
            // do something with response
        }
    });         

},
6个回答

13

尝试:

data: {
    start: startDateTime,
    end: endDateTime
}

这将在服务器上创建名为“start”和“end”的请求参数,您可以使用它们。

{...} 是一个对象字面量,它是创建对象的一种简单方法。 .ajax函数接受该对象,并将其属性(在本例中为“start”和“end”)转换为键值对,这些键值对被设置为发送到服务器的HTTP请求的属性。


7
data: {
    startDateTime : "xxx",
    endDateTime : "yyy"
}

3
您可以使用JSON表示法传递值:
data: {startDateTime: 'value here ', endDateTime: 'value here '}

1

尝试一下:

data: JSON.stringify({ start: 开始时间, end: 结束时间 })


0

在数据中

ajax({
    url : //your file url finshed with **,**
    data : {Start: Mon Jan 10 2011 18:15:00 GMT+0000 (GMT Standard Time),
           End: Mon Jan 10 2011 18:45:00 GMT+0000 (GMT Standard Time)}, //finish with **,**
    type: 'POST',
    success: function(response)
    {
        // do something with response
    }

});

0
ajax({
     url : //your file url finshed with ,
     data : {
         Start: Mon Jan 10 2011 18:15:00 GMT+0000 (GMT Standard Time),
         End: Mon Jan 10 2011 18:45:00 GMT+0000 (GMT Standard Time)
     },
     type: 'POST',
     success: function(response) { 
         // do something with response 
     }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接