I am trying to make a url shortener using goog.gl api. Thanks to @Barmar now I can get my short URL using this code:
var shortURL;
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl: "' + longURL +'"}',
dataType: 'json',
success: function(response) {
shortURL = response.id;
}
});
But I want to shorten an array of links! So I decided to use loop.
I created longURL[] and shortURL[] but if I run this code I get such output in shortURL array: [undefined × 10, "http://goo.gl/AxzWLx"];
Full code:
var longURL = [];//there are some urls
var shortURL = [];
for (var k = 0; k < longURL.length; k++) {
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl: "' + longURL[k] +'"}',
dataType: 'json',
success: function(response) {
shortURL[k] = response.id;
}
});
}