simple javascript jquery ajax call - 405 method not allowed

5,306 views
Skip to first unread message

Tamale

unread,
Feb 28, 2010, 4:50:40 PM2/28/10
to bitly API
Could someone please help me understand where I'm going wrong here?

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.

Jehiah Czebotar

unread,
Feb 28, 2010, 10:11:12 PM2/28/10
to bitl...@googlegroups.com
a few comments, even though it isn't clear exactly what response you
are getting. if you can include the contents of the response and an
exact url that is loaded that would be helpful. (or email to
sup...@bit.ly if you would like).

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

vhanla

unread,
Mar 1, 2010, 2:20:11 PM3/1/10
to bitl...@googlegroups.com
I don't know much, but it could be a crossdomain issue.

UIC Tamale

unread,
Mar 1, 2010, 2:45:13 PM3/1/10
to bitl...@googlegroups.com
That was my first guess too, but I've done something almost identical with tiny-url's API and it worked fine.

-josh


On Mon, Mar 1, 2010 at 1:20 PM, vhanla <vha...@gmail.com> wrote:
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

UIC Tamale

unread,
Mar 2, 2010, 11:12:37 AM3/2/10
to bitl...@googlegroups.com
Thank you! These modifications allowed me to make my call directly from within Javascript.

I'd love to know why the simple get and post requests were returning errors, but I'm mostly just happy this is working.

Thanks,

-Tamale

Gregory Tomlinson

unread,
Mar 4, 2010, 4:57:18 PM3/4/10
to bitl...@googlegroups.com
Tamale

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

Reply all
Reply to author
Forward
0 new messages