Why can't I call bit.ly API from JavaScript using jQuery's $.get function? - Stack Overflow

I tried to call bit.ly API using this jQuery script:$.get(';apiKey=R_0da49e0a9118ff35f52f629d2d71b

I tried to call bit.ly API using this jQuery script:

$.get(';apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&longUrl=www.wordpress', function(data) {
    alert(data);
});

but firebug said "405 Method Not Allowed". What's wrong? Thanks a lot.

I tried to call bit.ly API using this jQuery script:

$.get('http://api.bit.ly/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&longUrl=www.wordpress.', function(data) {
    alert(data);
});

but firebug said "405 Method Not Allowed". What's wrong? Thanks a lot.

Share Improve this question edited Mar 12, 2013 at 14:20 Rendicahya asked Jan 2, 2010 at 18:13 RendicahyaRendicahya 4,2858 gold badges40 silver badges56 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

As mentioned already, standard AJAX call do not work cross domain. Just use JSONP with $.getJSON() instead.

Here is an example how to get a shortened URL with Bitly API and jQuery:

function get_short_url(long_url, login, api_key, func)
{
    $.getJSON(
        "http://api.bitly./v3/shorten?callback=?", 
        { 
            "format": "json",
            "apiKey": api_key,
            "login": login,
            "longUrl": long_url
        },
        function(response)
        {
            func(response.data.url);
        }
    );
}

The following code could be used to get a short URL:

/*
Sign up for Bitly account at
 https://bitly./a/sign_up

and upon pletion visit
https://bitly./a/your_api_key/ 
to get "login" and "api_key" values
*/
var login = "LOGIN_HERE";
var api_key = "API_KEY_HERE";
var long_url = "http://www.kozlenko.info";

get_short_url(long_url, login, api_key, function(short_url) {
    console.log(short_url);
});

$.get do not support cross-domain GET.

You can use JSONP technique, and $.getJSON.

BTW, http:// should in the longUrl parameter of bit.ly API call. But it's not the main problem.

The reason you're seeing the 405 error is because you're violating the Same Origin Policy, which prevents retrieving data from a different domain, subdomain, or protocol.

The URL is not valid.

You have to put the http:// in front of the longUrl argument.

Edit

Some clarifications:

This url http://api.bit.ly/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&longUrl=http://www.wordpress. returns

{ "errorCode": 0, "errorMessage": "", "results": { "www.wordpress.": { "errorCode": 1206, "errorMessage": "URL you tried to shorten was invalid.", "statusCode": "ERROR" } }, "statusCode": "OK" }

this one: http://api.bit.ly/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&longUrl=http://www.wordpress. returns

{ "errorCode": 0, "errorMessage": "", "results": { "http://www.wordpress.": { "hash": "j1IP3", "shortKeywordUrl": "", "shortUrl": "http://bit.ly/6i1NkN", "userHash": "6i1NkN" } }, "statusCode": "OK" }

They probably expect a POST request rather than a GET.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744299482a4567427.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信