JQuery and Ajax call sina API to get short url code

  • 2020-03-30 01:39:05
  • OfStack

 
<!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( There is no online search ) : </label><br> 
<input value="211160679" id="app_key"><br><br> 
<span id="short"> Short url: </span><br><br> 
<span id="dlgcy"> Calling address: </span><br><br> 
<button type="button" id="btn">Click me</button><br> 
<!--<span id="link"> Web site: </span>--> 
<br><br><br> 
<a href="http://www.cnblogs.com/azure/archive/2012/08/29/WeiboAPI.html" target="_blank"> Reference article: with the short chain service as an example, discusses the free AppKey , certification exemption, Ajax Call sina weibo across domains 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 attribute value;
//var url = "https://api.weibo.com/2/short_url/shorten.json"; // The 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 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", //Using JSONP method for AJAX,json has cross-domain problems;
cache: false, 
success: function (data, status) { 
//Gets the information returned;
for(x in data.data.urls[0]) message += x+'='+data.data.urls[0][x]+'&'; 
alert("Data:n" + message + "nnStatus: " + status); 
short.append( data.data.urls[0].url_short + "<br>"); 
//$("#dlgcy").hide(); 
$("#dlgcy").text(cmd2); 
}, 
error: function(obj,info,errObj){ 
alert("$.ajax() Error occurred in: " + info); 
} 
}); 

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

(link: http://xiazai.jb51.net/201402/yuanma/jqajaxsinapi(jb51.net).rar)

Related articles: