javascript - Send jQuery $.data object through $.ajax GET - Stack Overflow

admin2025-04-21  0

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:

$('#myForm').find('input').each(function(index){ 
    myData = $.data($('#myForm'), $(this).attr('name'), $j(this).val());
});

...and then pump it through the 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.

How do you send data through ajax like this? Is there a better way?

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:

$('#myForm').find('input').each(function(index){ 
    myData = $.data($('#myForm'), $(this).attr('name'), $j(this).val());
});

...and then pump it through the 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.

How do you send data through ajax like this? Is there a better way?

Share Improve this question edited Dec 22, 2015 at 11:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 21, 2012 at 21:02 emcemc 5071 gold badge7 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Use the jQuery serialize(); method:

$.ajax({
    type:"GET",
    url: '/otherpage.php',
    data = $('#myForm').serialize(),
    error(function(){}),
    success(function(){});
});

http://api.jquery./serialize/

$.ajax({
    type:"GET",
    url: '/otherpage.php',
    data = $('#myForm').serialize(),
    error(function(){}),
    success(function(){});
});

Hope it'will help you.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745172238a288728.html

最新回复(0)