作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 spring 框架开发一个 J2EE 网站。我希望与 Twitter 分享我的网站,但使用 bit.ly API 无法成功。该函数生成 bit.ly 链接,但在 Twitter 的共享页面中我只看到完整链接。如何将 bit.ly 链接发送到 Twitter?
我从 firebug 得到的 bit.ly 响应:
BitlyCB.getBitlyUrl({"errorCode": 0, "errorMessage": "", "results": {"http://twitter.com/home?status=http://www.google.com": {"userHash": "dodUFu", "hash": "9KnUl2", "shortUrl": "http://bit.ly/dodUFu", "shortCNAMEUrl": "http://bit.ly/dodUFu", "shortKeywordUrl": ""}}, "statusCode": "OK"})
如果你尝试http://twitter.com/home?status=http://www.google.com你能理解我。
我的代码如下:
<script type="text/javascript" charset="utf-8"
src="http://bit.ly/javascript-api.js?version=latest&login=mylogin&apiKey=mykey"></script>
<a class="_ffShare_"
onclick="onlyShortenUrl('http://twitter.com/home?status=http://mypage');">
<img src="http://yakup-laptop:8080/images/theme/default/twitter.png"></img>
</a>
function onlyShortenUrl(longUrl){
//single shortener
BitlyCB.getBitlyUrl = function(data) {
var shortUrl = extractShortUrl(data);
window.open(shortUrl,'_blank');
return shortUrl;
}
return BitlyClient.call('shorten', {'longUrl': longUrl}, 'BitlyCB.getBitlyUrl');
}
function extractShortUrl(data){
//bitly util method probably not useful standalone
var shortUrl = '';
var first_result;
// Results are keyed by longUrl, so we need to grab the first one.
for (var r in data.results) {
first_result = data.results[r]; break;
}
for (var key in first_result) {
shortUrl = r ;
}
return shortUrl;
}
最佳答案
尝试改变:
for (var key in first_result) {
shortUrl = r ;
}
至
shortUrl = first_result.shortUrl;
如果这不起作用,请包含 first_result
的输出(在同一点):
console.log(first_result);
关于javascript - 如何将 bit.ly 用于 Twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3726890/
我是一名优秀的程序员,十分优秀!