最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论