jQuery.getJSON 导致“未捕获的语法错误:意外的标记:”适用于 youtube oembed
这是我想要做的:
$.getJSON('http://www.youtube.com/oembed?url=http://www.youtube.com/watch%3Fv%3DB-m6JDYRFvk&callback=?',
function(data) { console.log(data) });
当卷曲该 URL 时,我得到以下响应:
{
"provider_url": "http:\/\/www.youtube.com\/",
"title": "Coder Girl",
"html": "\u003cobject width=\"425\" height=\"344\"\u003e\u003cparam name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/B-m6JDYRFvk?version=3\"\u003e\u003c\/param\u003e\u003cparam name=\"allowFullScreen\" value=\"true\"\u003e\u003c\/param\u003e\u003cparam name=\"allowscriptaccess\" value=\"always\"\u003e\u003c\/param\u003e\u003cembed src=\"http:\/\/www.youtube.com\/v\/B-m6JDYRFvk?version=3\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"\u003e\u003c\/embed\u003e\u003c\/object\u003e",
"author_name": "dalechase",
"height": 344,
"thumbnail_width": 480,
"width": 425,
"version": "1.0",
"author_url": "http:\/\/www.youtube.com\/user\/dalechase",
"provider_name": "YouTube",
"thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/B-m6JDYRFvk\/hqdefault.jpg",
"type": "video",
"thumbnail_height": 360
}
但是当我尝试执行上述代码时,我得到一个 Uncaught SyntaxError: Unexpected token :
(Chrome)。看起来问题可能与正斜杠的转义有关,或者 jQuery 可能正在发送 JSONP
请求,但响应是纯 JSON
。
还有其他人遇到过这个问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Youtube(截至本回答时)oembed 不支持 JSONP 及其请求,因此您返回的内容是正确的...但这不是您需要的。 JSONP 调用需要如下所示:
...因为当前返回的是无效的 JavaScript(就其本身而言,这就是它的本质),就是这样JSONP 是如何工作的,响应实际上需要是可执行的 JavaScript。
只需将该代码直接放入页面中的
块中(在此处查看演示)。
我不确定您想要做什么,但如果您只是在嵌入部分之后,我推荐像 jQuery-oembed 来做到这一点。如果您需要数据...您需要在服务器上进行某些操作来处理 JSON,然后再从服务器获取数据。
Youtube (as of the time of this answer) oembed doesn't support JSONP with their requests, so what you're getting back is correct...but it's not what you need. What you need for JSONP calls would look like this:
...since what comes back currently is not valid JavaScript (by itself, and that's what it is), and that's how JSONP works, the response actually needs to be executable JavaScript.
You can get the same error by just plopping that code straight in your page in a
<script>
block (see a demo here).I'm not sure exactly what you're trying to do, but if you're just after the embedding piece, I recommend a plugin like jQuery-oembed to do that. If you're after the data...you'll need something on your server to process the JSON, then get the data from your server after that.
从版本 1.5 开始,您不再需要将 JSONP 与 jQuery 一起使用。尝试 $.ajax() 并设置 crossDomain:true 并删除所有 ?callback 包装器并查看它是否有效。这是一种更可靠的方法,而且语法也更清晰。
You don't need to use JSONP anymore with jQuery as of version 1.5. Try $.ajax() and set crossDomain:true and remove all your ?callback wrappers and see if it works. It's a much more reliable method and the syntax is much cleaner.
也许更好的选择是将跨域调用替换为对在您自己的服务器上运行的 Web 服务的调用,该服务器为您运行调用。我的猜测是 Chrome 阻止了跨域请求。
Perhaps a better alternative would be to replace your cross domain call with a call to a web service running on your own server that runs the call for you. My guess is Chrome is blocking the cross domain request.
字符串化结果对我有用。
Stringify the result works for me.