<div class="codetitle"> <span><a style="CURSOR: pointer" data="27227" class="copybut" id="copybut27227" onclick="doCopy('code27227')"><u>複製代碼</u></a></span> 代碼如下:</div> <div class="codebody" id="code27227"> <br> <br> <br> <br><meta charset="utf-8"> <br><title>取得新浪短網址</title> <br><script src="http://ajax.aspnetcdn.com/ajax/jQuery%20/jquery-1.8.0.js"></script> <br><!--<script type="text/javascript" src="js/jquery-2.1.0.min.js"></script> --> <br> <br> <br><label for="long">長網址:</label> <br><input value="http://baike.baidu. com/link?url=XLrVdYRThMvU_DlDT67v2wg9XYgG3xXvsB97WZFDz4psPORqGotkTiH1O5L1SGrD9Du-OSxWt_5E2KNtk01HhK" id="long"><brm label> <br><input value="211160679" id="app_key"><br><br> <br><span id="short">短網址:</span><br><br> <br><span id="dlgcy">呼叫位址:</span><br><br> <br><button type="button" id="btn">Click me</button><br> <br> <!--<span id="link">網址:</span>--> <br><br><br><br> <br><a href="http://www.cnblogs.com%20/azure/archive/2012/08/29/WeiboAPI.html" target="_blank">參考文章:以短鏈服務為例,探討免AppKey、免認證、Ajax跨域呼叫新浪微博API</a> <br><br><br> <br><a href="http://open.weibo.com/tools/console?uri=short_url/shorten&httpmethod=GET&key1=url_long&value1=http://open.weibo.com%20/qa" target="_blank">新浪API測試</a> <br> <br><br><script type="text/javascript"> <br>$(document).ready(function (){ <BR>$("#btn").click(function(){ <BR>var short = $("#short"); <BR>var long = $("#long").val( ); //取得屬性值;<BR>//var url = "https://api.weibo.com/2/short_url/shorten.json"; //官方API位址; <BR>var url2 = "http ://api.weibo.com/2/short_url/shorten.json"; <BR>var app_key = $("#app_key").val();//app_key無效可能會導致無反應; <BR>/ /var access_token; <BR>//var cmd = url "&url_long=" 長 "&access_token=" access_token; <BR>var cmd2 = url2 "?source=" app_key "&url_long=" long; <BR><br>var🎜>var~ message=""; <br>$.ajax({ //底層方法; <BR>url: cmd2, <BR>type: "GET", <BR>dataType: "jsonp", //使用JSONP方法進行AJAX ,json有跨域問題; <BR>cache: false, <BR>success: function (data, status) { <BR>//取得傳回的資訊; <BR>for(x in data.data.urls[ 0]) message = x '=' data.data.urls[0][x] '&'; <BR>alert("Data:n" message "nnStatus: " status); <BR>short.append( data .data.urls[0].url_short "<br>"); <BR>//$("#dlgcy").hide(); <BR>$("#dlgcy").text(cmd2); <BR>}, <BR>error: function(obj,info,errObj){ <BR>alert("$.ajax()發生錯誤:" info); <BR>} <BR>}); <BR> <br>}); <br>}); <BR></script> <br></brm> </div>