In my JS I am sending a get request to the bit.ly api to shorted a URL. The problem is I need the URL returned for use in the code.
Would it be best to use a synchronous request for this? As it stands any code after the XHR request that uses the bit.ly would fail because the response has not yet returned the short URL.
bitlyXHR.onreadystatechange = function() {
if (bitlyXHR.readyState == 4) {
if (bitlyXHR.status == 200) {
var obj = JSON.parse(bitlyXHR.responseText);
// Do something
}
}
};
bitlyXHR.open("GET", "http://api.bitly.com/v3/shorten?login=&apiKey=&longUrl=" + longURL + "&format=json");
bitlyXHR.send();
// Some code here that uses the short URL
You can do something like this: