简体   繁体   中英

Send jQuery $.data object through $.ajax GET

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?

Use the jQuery serialize(); method:

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

http://api.jquery.com/serialize/

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

Hope it'will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM