var shareSurl = escape( currentUrl );
var bitlyUrl = "http://api.bit.ly/shorten?
version=2.0.1&login=tamale&apiKey=R_blahblahblah&longUrl="+shareSurl;
$.post(bitlyUrl, function(response){
var data = eval( response );
$('#shareSpan').html( data.results.shortUrl );
});
doesn't matter if I use $.post or $.get, in both I'm getting a 405
method not allowed error in firebug, but if I click on the link
directly (from right within firebug) it works every time and I see my
json response and good, working shortened url right in the browser.
It must be something so simple, I'd love some help to figure out why
it's not working.
you should use encodeURIComponent() not escape() to prepare a string
for use as a uri component.
i also think you also want .getScript() not .post() or .get()
http://api.jquery.com/jQuery.getScript/
you also can't directly access "data.results.shortUrl" because of the
format of the response.
something like this should do the trick for you.
[javascript]
var shareUrl = encodeURIComponent( currentUrl );
var login = "...";
var apiKey = "...";
jQuery.getScript("http://api.bit.ly/shorten?version=2.0.1&login=" +
login + "&apiKey=" + apiKey + "&longUrl=" + shareUrl +
"&callback=BitlyCB.shortenresponse");
function BitlyCB.shortenresponse(data) {
var bitly_link = null;
for (var r in data.results) {
bitly_link = data.results[r]['shortUrl'];
break;
}
$('#shareSpan').html( bitly_link);
}
[/javascript]
--
Jehiah
I don't know much, but it could be a crossdomain issue.
--
You are subscribed to the bit.ly API discussion group.
To post, email to bitl...@googlegroups.com
For more options, visit http://groups.google.com/group/bitly-api
This was indeed a cross domain issue. You need to use jsonp
Here is more information on how jsonp works
http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
You could actually use the $.get() command, you just need to add
'jsonp' as the last argument, like so:
$.get( "http://api.bit.ly/shorten", { version:'2.0.1', login :
'your_bitly_name', apiKey : 'your_api_key', longUrl :'http://foo.com'
}, BitlyCB.shortenresponse, 'jsonp' )
http://api.jquery.com/jQuery.get/
thanks
-gregory
--
-gregory
g...@bit.ly