goog.gl api를 사용하여 URL 단축키를 만들려고합니다. @Barmar 덕분에이 코드를 사용하여 짧은 URL을 얻을 수 있습니다 :배열에 JSON 응답을 추가하는 중 오류가 발생했습니다.
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;
}
});
하지만 링크 배열을 단축하고 싶습니다! 그래서 루프를 사용하기로 결정했습니다. [undefined × 10, "http://goo.gl/AxzWLx"];
전체 코드 :
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;
}
});
}
두 번째 변형은 잘 작동하고 나를 더 아름답고 논리적으로 보입니다. –