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!'); } });
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.
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.
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.
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) { } }); } });
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*/ }, }); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With