Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax Request inside Ajax Request

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!');     } }); 
like image 737
Bias Tegaralaga Avatar asked Apr 10 '12 13:04

Bias Tegaralaga


People also ask

Can we execute run multiple Ajax request simultaneously in jQuery?

To iterate through the response, there is a callback function attached to it. This callback function gets executed once both the Ajax requests are finished. In case where multiple Deferred objects are passed to $. when() , it takes the response returned by both calls, and constructs a new promise object.

What happens when one Ajax call is still running and you send an another Ajax call before the data of first Ajax call comes back?

Since Ajax calls are asynchronous, the application will not 'pause' until an ajax call is complete, and simply start the next call immediately. JQuery offers a handler that is called when the call is successful, and another one if an error occurs during the call.

How can make Ajax call in jQuery?

The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request. Parameters: The list of possible values are given below: type: It is used to specify the type of request. url: It is used to specify the URL to send the request to.


2 Answers

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) {                 }         });     } }); 
like image 100
Tariq Avatar answered Sep 20 '22 19:09

Tariq


Call second ajax from 'complete'

Here is the example

   var dt='';    $.ajax({     type: "post",     url: "ajax/example.php",     data: 'page='+btn_page,     success: function(data){         dt=data;         /*Do something*/     },     complete:function(){         $.ajax({            var a=dt; // This line shows error.            type: "post",            url: "example.php",            data: 'page='+a,            success: function(data){               /*do some thing in second function*/            },        });     } }); 
like image 33
Nishad Up Avatar answered Sep 21 '22 19:09

Nishad Up