How to use jQuery to produce TinyURL

2019-07-30 00:57发布

我试图建立一个jQuery的功能,让我从其他一些链接,微博的原因(是的,微博)产生TinyURL的...我发现从詹姆斯Padolsey本教程,但我不能从获取响应回呼叫。

http://james.padolsey.com/javascript/create-a-tinyurl-with-jsonp/

function requestShortURL(longURL, success) {
    var API = 'http://reque.st/create.api.php?json&url=',
        URL = API + encodeURIComponent(longURL) + '&callback=?';
    console.log('tweet apit url: ' + URL);
    $.getJSON(URL, function(data){
        success && success(data.url);
    });
}

requestShortURL('http://www.mycompany.com', function(shortened){
    alert('new url: ' + shortened);
});

Answer 1:

嗯,似乎对我很好地工作:

function makeTinyUrl(url)
{
    $.getJSON('http://json-tinyurl.appspot.com/?url=' + url + '&callback=?', 
        function(data)
        { 
            alert(data.tinyurl); 
        }
    );
}

makeTinyUrl('http://stackoverflow.com/questions/1111171/how-to-use-jquery-to-produce-tinyurl');


文章来源: How to use jQuery to produce TinyURL