How to use jQuery to produce TinyURL
Asked Answered
P

1

5

I'm trying to build a jQuery function that will allow me to produce a TinyURL from some other link for micro blogging reasons (yes, twitter)... I found this tutorial from James Padolsey, but am not getting a response back from the call.

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);
});
Purine answered 10/7, 2009 at 18:22 Comment(1)
Check your Javascript console (Tools > Error Console in Firefox), or use Firebug's console - it may give a useful error messageKirbee
K
8

Hm that seems to work fine for me:

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

makeTinyUrl('https://mcmap.net/q/1996928/-how-to-use-jquery-to-produce-tinyurl');
Kristynkrock answered 10/7, 2009 at 18:50 Comment(2)
Think a proxy could be killing this? I've copied everything you have here, and don't get any alert...Purine
Well what does it output if you directly call the URL ( json-tinyurl.appspot.com/?url=http://stackoverflow.com/… ) in your browser? I just tried it and it alerted and the Browser returns this: test123({"tinyurl": "http:\/\/tinyurl.com\/m2befh", "ok": true})Kristynkrock

© 2022 - 2024 — McMap. All rights reserved.