JQuery與Ajax調用新浪API擷取短網址的代碼_jquery

來源:互聯網
上載者:User
複製代碼 代碼如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>擷取新浪短網址</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">長網址:</label>
<input value="http://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="http://www.cnblogs.com/azure/archive/2012/08/29/WeiboAPI.html" target="_blank">參考文章:以短鏈服務為例,探討免AppKey、免認證、Ajax跨域調用新浪微博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">新浪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 = "http://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>

代碼下載
相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。