jquery and Ajax invoke Sina API to get short URL code _jquery

Source: Internet
Author: User
Tags button type
Copy Code code as follows:

<!doctype html>
<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>-->
<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 (no searchable online):</label><br>
<input value= "211160679" id= "App_key" ><br><br>
<span id= "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 for example, Explore Appkey, authentication-free, 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 (); Gets the property 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 (); invalid//app_key may result in no reaction;
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 ({//Bottom method);
URL:CMD2,
Type: "Get",
DataType: "Jsonp",///using Jsonp method for Ajax,json with cross-domain problems;
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) {
An error occurred in alert ("$.ajax ():" + info);
}
});

});
});
</script>

Code download
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.