[英]AJAX - submit multiple POST data
I am desperately trying to submit multiple POST variables via AJAX, but just cant get manage to get the formatting right... Problem is that I have both a hardcoded / written action=problem_lookup
variable and a dynamic field input as $(this).val
and just cant manage to get both into one data string... 我拼命试图通过AJAX提交多个POST变量,但是无法设法让格式正确...问题是我有一个硬编码/写入的
action=problem_lookup
变量和一个动态字段输入为$(this).val
并且无法将两者合并为一个数据字符串......
this works well: 这很好用:
data: 'problem=' + $(this).val(),
This does not: 这不是:
data: { action: 'problem_lookup' , problem: $("problem").val() },
data: { action: 'problem_lookup' , problem: $(this).val() },
data: { action: problem_lookup, problem: $(this).val() },
I tried numerous formats from other threads and looked at the official jquery manual, but cant seem to get this figured out. 我从其他线程尝试了很多格式并查看了官方的jquery手册,但似乎无法解决这个问题。 Any help is appreciated.
任何帮助表示赞赏。
EDIT: 编辑:
full script below, tried the solutions posted so far but no success. 下面的完整脚本,尝试了迄今为止发布的解决方案但没有成功。
$("problem")
is a <select>
field (with Select2 running) hence shouldnt cause me so much frustration, especially since the original approach with data: 'problem=' + $(this).val(),
works fine. $("problem")
是一个<select>
字段(运行Select2)因此不应该让我如此沮丧,特别是因为最初的data: 'problem=' + $(this).val(),
方法data: 'problem=' + $(this).val(),
工作正常。
$(function () {
$('#problem').change(function () { // on change in field "problem"
var data = {
action: 'problem_lookup',
problem: $("problem").val()
}
$.ajax({ // launch AJAX connection
type: 'POST', // via protocol POST
url: 'ajax.php',
//data: 'problem=' + $(this).val(), // send $_POST string
//data:"{'action':'"+action+"','problem':'"+$(this).val()+"'}",
//data:"{'action':'problem_lookup','problem':'"+$(this).val()+"'}",
//data: { action: 'problem_lookup' , problem: $("problem").val() },
//data : data_string,
data: $.param(data),
dataType: 'json', // encode with JSON
success: function (data)
{
// do something
},
});
});
});
You need to specify your data variable first like this: 您需要首先指定数据变量,如下所示:
var data = {
action: 'problem_lookup',
problem: $("problem").val()
}
In AJAX serialize your data using $.param , 在AJAX中使用$ .param序列化您的数据,
data: $.param(data),
Note: Twice check if $("problem").val()
is correct. 注意:两次检查
$("problem").val()
是否正确。 If problem
is a class, you need to specify like this $(".problem").val()
or if it is ID, $("#problem").val()
如果
problem
是一个类,你需要像这样指定$(".problem").val()
或者如果它是ID, $("#problem").val()
An issue is in the 一个问题在于
$("problem")
Jquery call. Jquery电话。
If.problem is a css class try with if.problem是一个css类试试
$(".problem")
if problem is a css id try with 如果问题是css id试试
$("#problem")
For posting arrays of object you can build data as an object containing arrays, changing a little bit your structure. 对于发布对象数组,您可以将数据构建为包含数组的对象,从而改变您的结构。 Something like this
像这样的东西
Var obj={};
obj.postData=[];
obj.postData.push(/*your first object here*/);
...
obj.postData.push(/*your n-th object here*/);
$.ajax({
.....
data:obj;
......
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.