在javascript中处理youtube数据api中的错误 [英] handling errors in youtube data api in javascript

查看:118
本文介绍了在javascript中处理youtube数据api中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成功加载之一就是:

http://gdata.youtube.com/feeds/api/videos/N7m86aMNjlQ?callback=jsonp1335008664824&alt=json-in-script

然而,当视频是私有或不存在时,它只返回一条带有http错误状态代码的消息。

However, when the video is private or doesn't exist, it returns just a message with an http error status code.

http:/ /gdata.youtube.com/feeds/api/videos/zkZBEnBjOjY?callback=jsonp1335008664915&alt=json-in-script

我创建了一个独特的示例回调函数名称,如jsonp1335008664824,并将url附加为脚本标记的src,并在脚本加载时捕获该函数。但是,如果它返回错误,youtube不会将内容作为回调函数返回,我无法捕获它们中的任何一个。因为脚本标签没有onerror属性,我甚至不知道它是加载还是返回了404或503状态代码之类的错误。

I create an example unique callback function name like "jsonp1335008664824" and append the url as src for a script tag and catch the function when the script loaded. However if it returns an error, youtube doesn't return the content as a callback function and I can't catch any of them. Because script tag doesn't have "onerror" attribute, I don't even know whether it's loading or returned an error like 404 or 503 status codes.

似乎是只有在处理脚本标签时才设置超时功能。然而,这不是一个可靠的方法,所以我正在寻找另一种选择。我知道,即使我可以捕获状态代码,我也无法收到错误消息,但正如我所见,只有两个状态代码和两个错误,404为未找到视频或503为视频是私有。

It seems the only whay to handle it is setting timeout function when I append script tag. However it's not a reliable way to do that so I'm looking for another option. I know that even I can catch the status code, I can't get error message but as I see, there's only two status code and two errors that are 404 for "Video not found" or 503 for "Video is private".

推荐答案

我从Youtube开发者论坛得到了答案。因此,jsonc是处理这类请求的更好方法。而这个开发请求变成了这种布局:
http://gdata.youtube.com/feeds/api/videos/zkZBEnBjOjY?callback=jsonp1335008664915&alt=jsonc&v=2

I got the answer from Youtube Developer Forum. As a result, jsonc is a better way to handle these kind of request. And this this development the requests became this layout: http://gdata.youtube.com/feeds/api/videos/zkZBEnBjOjY?callback=jsonp1335008664915&alt=jsonc&v=2

我也从这里了解到,我们可以在Youtube Data Api中使用XMLHTTPRequest。这很棒,因为我们可以从现在开始发出POST和PUT请求。

And also as I learned from here, we can use XMLHTTPRequest in Youtube Data Api. This is awesome because we can make POST and PUT request from now on.

这篇关于在javascript中处理youtube数据api中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆