jQuery.getJSON 导致“未捕获的语法错误:意外的标记:”适用于 youtube oembed

发布于 2024-11-18 09:09:41 字数 1441 浏览 4 评论 0 原文

这是我想要做的:

$.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

还有其他人遇到过这个问题吗?

Here is what I'm trying to do:

$.getJSON('http://www.youtube.com/oembed?url=http://www.youtube.com/watch%3Fv%3DB-m6JDYRFvk&callback=?', 
           function(data) { console.log(data) });

When curling that URL I get this response:

{
    "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
}

But when I try to execute the above code, I get an Uncaught SyntaxError: Unexpected token : (Chrome). It looks like the problem might have something to do with the escaping of the forward slashes, or maybe that jQuery is sending a JSONP request, but the response is pure JSON.

Has anyone else run into this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

香草可樂 2024-11-25 09:09:41

Youtube(截至本回答时)oembed 不支持 JSONP 及其请求,因此您返回的内容是正确的...但这不是您需要的。 JSONP 调用需要如下所示:

functionName({
    "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
});

...因为当前返回的是无效的 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:

functionName({
    "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
});

...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.

裂开嘴轻声笑有多痛 2024-11-25 09:09:41

从版本 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.

我纯我任性 2024-11-25 09:09:41

也许更好的选择是将跨域调用替换为对在您自己的服务器上运行的 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.

长梦不多时 2024-11-25 09:09:41

字符串化结果对我有用。

$.getJSON('your_url_here', function(data) {
    console.log(data);  //"Object { Json content }"
    var jsonString = JSON.stringify(data);  //"{ Json content }", the "Object" literal is removed.
    var parsed = JSON.parse(jsonString); //Parse should work now.
});

Stringify the result works for me.

$.getJSON('your_url_here', function(data) {
    console.log(data);  //"Object { Json content }"
    var jsonString = JSON.stringify(data);  //"{ Json content }", the "Object" literal is removed.
    var parsed = JSON.parse(jsonString); //Parse should work now.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文