JQuery与Ajax调用新浪API获取短网址的代码
代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>获取新浪短网址</title>
<script src="https://ajax.netcdn.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">长网址:</label>
<input value="https://baike.baidu.com/link?url=xlrvdyrthmvu_dldt67v2wg9xygg3xxvsb97wzfdz4psporqgotktih1o5l1sgrd9du-osxwt_5e2kntk01hhk" id="long"><br><br>
<label for="app_key">输入您的app_key(没有可到网上搜索):</label><br>
<input value="211160679" id="app_key"><br><br>
<span id="short">短网址:</span><br><br>
<span id="dlgcy">调用地址:</span><br><br>
<button type="button" id="btn">click me</button><br>
<!--<span id="link">网址:</span>-->
<br><br><br>
<a href="https://www.cnblogs.com/azure/archive/2012/08/29/weiboapi.html" target="_blank">参考文章:以短链服务为例,探讨免appkey、免认证、ajax跨域调用新浪微博api</a>
<br><br>
<a href="https://open.weibo.com/tools/console?uri=short_url/shorten&httpmethod=get&key1=url_long&value1=http%3a%2f%2fopen.weibo.com%2fqa" target="_blank">新浪api测试</a>
</body>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
var short = $("#short");
var long = $("#long").val(); //获取属性值;
//var url = "https://api.weibo.com/2/short_url/shorten.json"; //官方api地址;
var url2 = "https://api.weibo.com/2/short_url/shorten.json";
var app_key = $("#app_key").val();//app_key无效可能会导致无反应;
//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({ //底层方法;
url: cmd2,
type: "get",
datatype: "jsonp", //使用jsonp方法进行ajax,json有跨域问题;
cache: false,
success: function (data, status) {
//获取传回的信息;
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("$.ajax()中发生错误:" + info);
}
});
});
});
</script>
</html>
下一篇: 麒麟820E悄然发布:华为第一次六核心