如何使用JavaScript JQuery的Bitly API创建URL Shortener? [英] How to make a URL Shortener with the Bitly API with JavaScript JQuery?

查看:543
本文介绍了如何使用JavaScript JQuery的Bitly API创建URL Shortener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Bitly API为我的Web应用程序创建一个URL Shortener工具。

I'm trying to make a URL Shortener tool for my web application using the Bitly API.

我找到了这里有一个SO线程,看到了jQuery解决方案似乎很简单。

I found an SO thread here and saw what seemed to be simple enough jQuery solution.

getShortUrl: function(url, callback){
   var accessToken = //My access token, I've signed up at the site
   var url = 'https://api-ssl.bitly.com/v3/shorten?access_token=' + accessToken + '&longUrl=' + encodeURIComponent(url);

$.getJSON(
    url,
    {},
    function(response)
    {
        if(callback)
            callback(response.data.url);
    }
);
},

alert(getShortUrl("https://stackoverflow.com/search?q=URL+Shortener+JavaScript"))

我正在使用chrome dev工具测试这个。代码不会出错,但警报总是以未定义的形式出现。

I'm testing this out in chromium dev tools. The code doesn't error but the alert always comes out as undefined.

推荐答案

你没有传递回调而你得到 undefined 因为 getShortUrl 不会返回任何内容。

You aren't passing a callback and you get undefined because getShortUrl doesn't return anything.

试试这个:

getShortUrl("http://stackoverflow.com/search?q=URL+Shortener+JavaScript",
    function (url) {
        alert(url);
    }
);

这篇关于如何使用JavaScript JQuery的Bitly API创建URL Shortener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆