2009-07-10 19 views
5

我正在尝试构建一个jQuery函数,该函数将允许我从其他一些链接生成一个TinyURL用于微博客的原因(是的,微博)...我发现本教程来自James Padolsey ,但我没有收到回应。如何使用jQuery生成TinyURL

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); 
}); 
+0

检查你的Javascript控制台(工具>错误控制台在Firefox),或使用Firebug的控制台 - 这可能会给一个有用的错误消息 – dbr 2009-07-10 22:01:23

回答

8

嗯,似乎为我工作的罚款:

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'); 
+0

认为代理可能会造成这种情况?我已经复制了您在这里的所有内容,并且不会收到任何警报... – RSolberg 2009-07-10 18:57:09