views:

397

answers:

5

I've looked through simiilar questions on SO, but can't seem to find one addressing what seems like a simple call..

function TweetThis(url)
{
    $.ajax({
      url: "http://tinyurl.com/api-create.php?url=" + url,
      cache: false,
      success: function(data){
       alert(data);
      }
    });
}

Basically I want to call TinyURL with an Ajax call and a long URL and return the shortened URL.. The success never fires, but when I check the URL it builds it returns fine in a browser.

Looking in Firebug it doesn't show response coming back.. what am I missing?

+1  A: 

In Safari 4 (Mac OS X), it works fine.
In Firefox 3 (Mac OS X), it half works - a alert dialog comes up, but it's empty (so success is firing, but no data is returned).
It seems to be a Firefox bug then.

Isaac Waller
Not working on FF3 for me on Windows... I thought perhaps it was a problem with TinyURL but tried another provider with the same results.
stonedonkey
+1  A: 

This should work

function TweetThis(url){
    $.get(
        "http://tinyurl.com/api-create.php",
        {url: url},
        function(data){
            alert(data);
        }
    );
}
SeanJA
A: 

I am having the same issue. jQuery.get works when I call any other url, but the tinyurl above does not respond at all. It does respond appropriately, however, if I place it in the address bar of the browser...the same issue happens in IE, so I do not think it is Fx.

Gil Carroll
A: 

Have you tried adding "&callback=?" to the end of the URL? It could be the browser security getting in the way.

T. Stone
A: 

Attempting to make a regular AJAX request is impossible because of same origin policy restrictions. Luckily there's a JSONP API courtesy of Remy Sharp.

Here's working code:

function TweetThis(bigurl)
{
    $.getJSON(
      "http://json-tinyurl.appspot.com/?&callback=?",
      {url: bigurl},
      function(data){
       alert(data.tinyurl);
      }
    );
}
brianpeiris