Interestingly, others consider the best way for an architect to create an API call, which depends on the response of another API call in jQuery.
Steps:
- Make JSONP API call endpoint, get response
- If we get 200 successful responses from the first call, we will start another API call (JSON this time).
- Print the results to the browser.
Here's how I could build it with rude error handling:
$(document).ready(function() {
$.ajax({
url: "http://example.com/json",
type: 'POST',
dataType: 'jsonp',
timeout: 3000,
success: function(data) {
var userLocation = data.loc;
var userRegion = data.city;
$.ajax({
url: "http://example2.com/json?Location=" + userLocation + "&City=" + userRegion,
type: 'POST',
dataType: 'json',
timeout: 3000,
success: function(Response) {
$(.target-div).html(Response.payload);
},
error: {
alert("Your second API call blew it.");
}
});
},
error: function () {
alert("Your first API call blew it.");
}
});
});
source
share