jQuery 如何在jquery ajax调用中动态传递数据参数(多个数据数组)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13109180/
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
How to dynamically pass data parameter in a jquery ajax call (multiple arrays of data)
提问by DominicM
I have this ajax code:
我有这个ajax代码:
return $.ajax({
type: "POST",
url: "somefile.php",
cache:false,
data: { "yfilter": $("#yearFilter").serializeArray(),
"gfilter": $("#genreFilter").serializeArray() },
dataType:"json",
success: function(data){
alert("success");
}
This works fine, but I need to pass the data parameter dynamically. For now I need the above data parameter content and a single string.
这工作正常,但我需要动态传递数据参数。现在我需要上面的数据参数内容和一个字符串。
How do I pass this dynamically? / How do I store it in a variable and pass it to the "data:" field?
我如何动态传递这个?/ 如何将其存储在变量中并将其传递给“数据:”字段?
{ "yfilter": $("#yearFilter").serializeArray(),
"gfilter": $("#genreFilter").serializeArray() }
I tried JSON.stringify I I couldn't get it to work probably due to the data being an array.
我试过 JSON.stringify II 可能因为数据是一个数组而无法让它工作。
The year and genre arrays are coming directly from the jquery drop down menu. It is selected like by it's #id like so "$("#yearFilter")". This is the select form element.
年份和流派数组直接来自 jquery 下拉菜单。它是通过它的#id 来选择的,就像这样“$("#yearFilter")”。这是选择表单元素。
<select multiple="multiple" name="yearFilter[]" class="filterChange" id="yearFilter">
What I need at the base level is:
我在基础级别需要的是:
var someData = "";
if(...){
someData = { "yfilter": $("#yearFilter").serializeArray(),
"gfilter": $("#genreFilter").serializeArray() };
}
else if(...){
someData = "sampleString";
}
And in ajax call:
在 ajax 调用中:
...
data: someData,
...
回答by charlietfl
I think I have an idea what you want but post has been overly complicated by extraneous issues like json stringify . Here's a function that could be used in several places eleswhere in your code to make one type of AJAX call or another.
我想我知道你想要什么,但是 post 被 json stringify 这样的无关紧要的问题过于复杂了。这是一个可以在代码中的多个地方使用的函数,以进行一种或另一种类型的 AJAX 调用。
You would then perhaps have several buttons and call the function within handlers for each type of button and change the argument passed to function
然后,您可能会有几个按钮,并在每种类型的按钮的处理程序中调用该函数,并更改传递给函数的参数
doAjax('someval');/* use in button 1*/
doAjax('someOtherval');/* use in button 2*/
function doAjax(arg) {
var someData = "";
if (arg == 'someval') {
someData = {
"yfilter": $("#yearFilter").val(),
"gfilter": $("#genreFilter").val()
};
} else {
someData = "sampleString";
}
$.ajax({
type: "POST",
url: "somefile.php",
cache: false,
data: someData,
dataType: "json",
success: function(data) {
if (arg == 'someval') {
alert("success 1");
} else {
alert("success 2");
}
}
})
}
回答by jan267
Hope I've understood what you're asking for. You could do something like this:
希望我已经明白你在问什么。你可以这样做:
var parameters = {};
if (...) {
parameters = $("#yearFilter").serializeArray();
}
if () {
parameters = $("#genreFilter").serializeArray();
}
and then replace the line:
然后替换该行:
parameters: { "yfilter": $("#yearFilter").serializeArray(),
"gfilter": $("#genreFilter").serializeArray() },
with:
和:
data: parameters,
回答by bijayk
JSON type should be best option for dynamically data. push whatever data you wish to inside json like shown below, Hence create your json dynamically and send as ajax data.
JSON 类型应该是动态数据的最佳选择。将您想要的任何数据推送到 json 中,如下所示,因此动态创建您的 json 并作为 ajax 数据发送。
var employees = {
accounting: [],
dept: "HR"
};
employees.accounting.push({
"firstName" : item.firstName,
"lastName" : item.lastName,
"age" : item.age
});
$.ajax({
url: POSTURL,
type: 'POST',
dataType: 'json',
data : employees,
contentType: 'application/json; charset=utf-8',
success: function (results)
{
},
error: function (results)
{
jQuery.error(String.format("Status Code: {0}, ", results.status, results.statusText));
}
});
回答by Brijesh Kansagara
Try it:
尝试一下:
someData = JSON.stringify({ yfilter: $("#yearFilter").val(), gfilter: $("#genreFilter").val() });
It will work.
它会起作用。