jquery and Ajax invoke Sina API to get short URL code

Source: Internet
Author: User
Tags button type json


  This article mainly introduces the call Sina API to get a short URL of a lot of methods, this example for you to use jquery and Ajax to achieve, the need for friends can refer to the following


code show as below:

<! doctype html>
<html>
<head>
<meta charset = "utf-8">
<title> Get Sina short URL </ title>
<script src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"> </ script>
<!-<script type = "text / javascript" src = "js / jquery-2.1.0.min.js"> </ script>->
</ head>
<body>
<label for = "long"> Long URL: </ label>
<Input value = "http://baike.baidu.com/link?url=XLrVdYRThMvU_DlDT67v2wg9XYgG3xXvsB97WZFDz4psPORqGotkTiH1O5L1SGrD9Du-OSxWt_5E2KNtk01HhK" id = "long"> <br> <br>
<label for = "app_key"> Enter your app_key (not available for online search): </ label> <br>
<Input value = "211160679" id = "app_key"> <br> <br>
<Span id = "short"> Short URL: </ span> <br> <br>
<Span id = "dlgcy"> call address: </ span> <br> <br>
<button type = "button" id = "btn"> Click me </ button> <br>
<!-<span id = "link"> URL: </ span>->
<br> <br> <br>
<a href="http://www.cnblogs.com/azure/archive/2012/08/29/WeiboAPI.html" target="_blank"> Reference article: Take short chain service as an example to discuss free AppKey and free Authentication, Ajax cross-domain call Sina Weibo API </a>
<br> <br>
<a href="http://open.weibo.com/tools/console?uri=short_url/shorten&httpmethod=GET&key1=url_long&value1=http%3A%2F%2Fopen.weibo.com%2Fqa" target="_blank"> Sina API test </a>
</ body>

<script type = "text / javascript">
$ (document) .ready (function () {
$ ("# btn"). click (function () {
var short = $ ("# short");
var long = $ ("# long"). val (); // Get attribute value;
// var url = "https://api.weibo.com/2/short_url/shorten.json"; // Official API address;
var url2 = "http://api.weibo.com/2/short_url/shorten.json";
var app_key = $ ("# app_key"). val (); // app_key invalid may cause no response;
// var access_token;
// var cmd = url + "& url_long =" + long + "& access_token =" + access_token;
var cmd2 = url2 + "? source =" + app_key + "& url_long =" + long;

var message = "";
$ .ajax ({// The underlying method;
url: cmd2,
type: "GET",
dataType: "jsonp", // Use JSONP method for AJAX, json has cross-domain issues;
cache: false,
success: function (data, status) {
// Get the returned information;
for (x in data.data.urls [0]) message + = x + '=' + data.data.urls [0] [x] + '&';
alert ("Data: \ n" + message + "\ n \ nStatus:" + status);
short.append (data.data.urls [0] .url_short + "<br>");
// $ ("# dlgcy"). hide ();
$ ("# dlgcy"). text (cmd2);
},
error: function (obj, info, errObj) {
alert ("An error occurred in $ .ajax ():" + info);
}
});

});
});
</ script>
</ html>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.