利用新浪API生成网址短链接

js
var convertStr = encodeURIComponent(urlStr); //转码
var appkey = 'xxx';
getShortUrl(convertStr);
function getShortUrl(urlStr) {
  $.ajax({
    type: 'GET',
    url:
      'http://api.t.sina.com.cn/short_url/shorten.json?source=' + appkey + '&url_long=' +
      urlStr, //source为新浪appkey
    dataType: 'JSONP',
    json: 'callback',
    data: {},
    success: function(r) {
      return r[0].url_short; //生成的短链接
    }
  });
}
收藏 打印