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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4663341/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:40:49  来源:igfitidea点击:

How to pass multiple JavaScript data variables in a jQuery ajax() call?

jqueryajaxparameter-passing

提问by Tony Gilroy

If startDateTime& endDateTimehave are dateTime values along the lines of this:

如果startDateTime& endDateTimehave 是这样的日期时间值:

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)

How do you pass both startDateTime& endDateTimeto the ajax call below?

你如何将startDateTime&传递endDateTime给下面的 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
        }
    });         

},

回答by Rob Hruska

Try:

尝试:

data: {
    start: startDateTime,
    end: endDateTime
}

This will create request parameters of 'start' and 'end' on the server that you can use.

这将在您可以使用的服务器上创建“开始”和“结束”的请求参数。

The {...}is an object literal, which is an easy way to create objects. The .ajaxfunction takes the object and translates its properties (in this case, 'start' and 'end') into key/value pairs that are set as properties on the HTTP request that gets sent to the server.

{...}是一个对象文字,这是创建对象的简单方法。该.ajax函数获取对象并将其属性(在本例中为“开始”和“结束”)转换为键/值对,这些键/值对设置为发送到服务器的 HTTP 请求的属性。

回答by Cesar

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

回答by Jose Basilio

You can pass the values in JSON notation:

您可以以 JSON 表示法传递值:

data: {startDateTime: 'value here ', endDateTime: 'value here '}

回答by Brijesh Kansagara

Try it:

尝试一下:

data: JSON.stringify({ start: startDateTime, end: endDateTime })

数据:JSON.stringify({开始:开始日期时间,结束:结束日期时间})

回答by mustafa omar

in the data

在数据中

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
    }

});

回答by mustafa omar

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 
     }
});