jQuery Ajax Request inside Ajax Request

Bias Tegaralaga picture Bias Tegaralaga · Apr 10, 2012 · Viewed 132.3k times · Source

Is it possible to make an ajax request inside another ajax request? because I need some data from first ajax request to make the next ajax request.

First I'm using Google Maps API to get LAT & LNG, after that I use that LAT & LNG to request Instagram API (search based location).

Once again, is this possible, and if so how?

$('input#search').click(function(e){
    e.preventDefault();
    var source=$('select[name=state] option:selected').text()+' '+$('select[name=city] option:selected').text()+' '+$('select[name=area] option:selected').text();
    var source=source.replace(/ /g, '+');
    if(working==false){
        working=true;
        $(this).replaceWith('<span id="big_loading"></span>');
        $.ajax({
            type:'POST',
            url:'/killtime_local/ajax/location/maps.json',
            dataType:'json',
            cache: false,
            data:'via=ajax&address='+source,
            success:function(results){
                // this is where i get the latlng
            }
        });
    } else {
        alert('please, be patient!');
    }
});

Answer

Tarek picture Tarek · Apr 10, 2012

Here is an example:

$.ajax({
        type: "post",
        url: "ajax/example.php",
        data: 'page=' + btn_page,
        success: function (data) {
            var a = data; // This line shows error.
            $.ajax({
                type: "post",
                url: "example.php",
                data: 'page=' + a,
                success: function (data) {

                }
            });
        }
    });