【问题标题】:How to use jQuery to produce TinyURL如何使用 jQuery 生成 TinyURL
【发布时间】:2010-11-09 19:36:20
【问题描述】:

我正在尝试构建一个 jQuery 函数,该函数允许我出于微博原因(是的,twitter)从其他链接生成 TinyURL...我从 James 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);
});

【问题讨论】:

  • 检查您的 Javascript 控制台(Firefox 中的工具 > 错误控制台),或使用 Firebug 的控制台 - 它可能会提供有用的错误消息

标签: jquery jquery-plugins tinyurl


【解决方案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');

【讨论】:

  • 认为代理可能会杀死这个?我已经复制了您在这里的所有内容,但没有收到任何警报...
  • 如果您在浏览器中直接调用 URL (json-tinyurl.appspot.com/?url=http://stackoverflow.com/…),它会输出什么?我刚试了一下,它发出警报,浏览器返回:test123({"tinyurl": "http:\/\/tinyurl.com\/m2befh", "ok": true})
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 2011-01-26
  • 2010-11-27
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
相关资源
最近更新 更多