问题
One of the successful load is like that:
http://gdata.youtube.com/feeds/api/videos/N7m86aMNjlQ?callback=jsonp1335008664824&alt=json-in-script
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
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.
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".
回答1:
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
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.
来源:https://stackoverflow.com/questions/10258882/handling-errors-in-youtube-data-api-in-javascript