<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/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"><br><br> <br><label for="app_key">输入您的app_key(没有可到网上搜索):</label><br> <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/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%3A%2F%2Fopen.weibo.com%2Fqa" 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=" + long + "&access_token=" + access_token; <BR>var cmd2 = url2 + "?source=" + app_key + "&url_long=" + long; <br><br>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 + "\n\nStatus: " + 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> <br> </div> <br><a href="http://xiazai.jb51.net/201402/yuanma/jqajaxsinapi(jb51.net).rar" target="_blank">代码下载</a>