I have a form with a pile of input fields. I want to make a ajax GET request with all of the fields! Easiest way so far looks like assigning the inputs to a data object:
我有一个带有一堆输入字段的表单。我想用所有字段制作一个ajax GET请求!到目前为止,最简单的方法就是将输入分配给数据对象:
$('#myForm').find('input').each(function(index){
myData = $.data($('#myForm'), $(this).attr('name'), $j(this).val());
});
...and then pump it through the ajax:
...然后通过ajax泵送它:
$.ajax({
type:"GET",
url: '/otherpage.php',
data = myData,
error(function(){}),
success(function(){});
});
But of course it doesn't work... no $_GET variables show up in the otherpage.php, and the console shows that myData
is some huge object deal.
但当然它不起作用...没有$ _GET变量显示在otherpage.php中,并且控制台显示myData是一个巨大的对象交易。
How do you send data through ajax like this? Is there a better way?
你如何通过像这样的Ajax发送数据?有没有更好的办法?
2 个解决方案
#1
6
Use the jQuery serialize();
method:
使用jQuery serialize();方法:
$.ajax({
type:"GET",
url: '/otherpage.php',
data = $('#myForm').serialize(),
error(function(){}),
success(function(){});
});
#2
1
$.ajax({
type:"GET",
url: '/otherpage.php',
data = $('#myForm').serialize(),
error(function(){}),
success(function(){});
});
Hope it'will help you.
希望它能帮到你。
#1
6
Use the jQuery serialize();
method:
使用jQuery serialize();方法:
$.ajax({
type:"GET",
url: '/otherpage.php',
data = $('#myForm').serialize(),
error(function(){}),
success(function(){});
});
#2
1
$.ajax({
type:"GET",
url: '/otherpage.php',
data = $('#myForm').serialize(),
error(function(){}),
success(function(){});
});
Hope it'will help you.
希望它能帮到你。