使用 jQuery $.getJSON 处理 Twitter API 错误
$.getJSON(twitter_url, function(data){
loadtwit(data);
});
我通过 $.getJSON 查询 twitter url,例如: http://twitter.com/statuses/friends/stevejobs.json?callback=?
有时,如果推文受到保护,浏览器响应将是未经授权的。我如何处理该响应以避免触发登录表单。
成功:loadtwit(数据);
else: 默默地死去
$.getJSON(twitter_url, function(data){
loadtwit(data);
});
I am querying the twitter url via $.getJSON, for instance:
http://twitter.com/statuses/friends/stevejobs.json?callback=?
Sometimes the browser response is UnAuthorized if the tweets are protected. How can I handle that response to avoid triggering a login form.
success: loadtwit(data);
else: die silently
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
$.ajax
并提供错误处理程序。You can use
$.ajax
and provide an error handler.我想我找到了一些接近解决方法的东西,即使不是最终的答案。
就我而言,我想根据我知道的用户名显示用户的位置、姓名、照片和一些推文,所以我尝试使用这个:
http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=stevejobs&count =20&回调=?
当查询具有受保护推文的用户时,这会触发看起来像网络钓鱼诈骗的弹出窗口。
因此,您可以根据用户查询推文搜索,如下所示:
http ://search.twitter.com/search.json?q=from:stevejobs&callback=?
您还可以像这样查询用户:
http:// api.twitter.com/1/users/show/stevejobs.json?&callback=?
第一个查询将取回推文,并且不会要求输入密码,因为受保护的用户不会出现在搜索中。有效,但不会返回用户的位置和元数据。
第二个查询不返回推文,但返回受保护的布尔值。
所以如果你把它们放在一起,你就可以得到完整的搜索。
我希望有人觉得这很有用。我一整天都在谷歌上搜索并阅读 API。写完函数后,我会回到这里并将其发布。
I think I found something close to a workaround, if not an answer finally.
In my case I wanted to show a user's location, name, photo and some tweets based on their username which I knew, so I was trying to use this:
http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=stevejobs&count=20&callback=?
Which trigger the popup that looks like a phishing scam when querying users with protected tweets.
So you can query a search for tweets based on a user like this:
http://search.twitter.com/search.json?q=from:stevejobs&callback=?
And you can also query a user like this:
http://api.twitter.com/1/users/show/stevejobs.json?&callback=?
The first query will get back tweets and won't ask for passwords because the protected users do not appear in search. Works, but doesn't return location and meta data for the user.
The 2nd query doesn't return tweets, but does return a boolean value for protected.
So if you put it together, you can get a complete search.
I hope someone finds this useful. I've been googling and reading the API all day. After I write the function, I'll come back here and post it.