Jquery Ajax请求在safari中有效,但在Firefox(Vimeo oembed)中无效
我试图通过oEmbedAPI(json)获得vimeo嵌入代码 它在safari中运行良好,但在Firefox中,返回的json似乎没有得到正确解释,因为我得到的是一个空值,而不是javascript对象(在success方法中) 我会给出一个指向JSFIDLE示例的链接,但是该示例在那里不起作用,关于不允许的源代码的一些错误 下面是代码:Jquery Ajax请求在safari中有效,但在Firefox(Vimeo oembed)中无效,jquery,ajax,json,firefox,oembed,Jquery,Ajax,Json,Firefox,Oembed,我试图通过oEmbedAPI(json)获得vimeo嵌入代码 它在safari中运行良好,但在Firefox中,返回的json似乎没有得到正确解释,因为我得到的是一个空值,而不是javascript对象(在success方法中) 我会给出一个指向JSFIDLE示例的链接,但是该示例在那里不起作用,关于不允许的源代码的一些错误 下面是代码: <script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
$.ajax({
url: "http://vimeo.com/api/oembed.json?&format=json&url=http%3A//vimeo.com/2197639",
dataType: "json",
success: function(data) {
$('#output').html(JSON.stringify(data));
},
error: function(errorSender, errorMsg) {
console.log(errorSender);
console.log(errorMsg);
$('#output').html(errorSender + ' ' + errorMsg);
}
});
});
//]]>
</script>
//
有什么问题吗?这与json有关吗
示例json是:
{"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"http:\/\/vimeo.com\/","title":"Early Morning Qena","author_name":"Oliver Wilkins","author_url":"http:\/\/vimeo.com\/offshoot","is_plus":"1","html":"<iframe src=\"http:\/\/player.vimeo.com\/video\/2197639\" width=\"1280\" height=\"720\" frameborder=\"0\" webkitAllowFullScreen allowFullScreen><\/iframe>","width":1280,"height":720,"duration":229,"description":"Early morning in Quft, near Qena. Shot with EX1 and Letus Extreme 35mm DOF adaptor.\n\nwww.offshoot.tv\n","thumbnail_url":"http:\/\/b.vimeocdn.com\/ts\/271\/854\/27185484_640.jpg","thumbnail_width":640,"thumbnail_height":360,"video_id":2197639}
{“type”:“video”,“version”:“1.0”,“provider\u name”:“Vimeo”,“provider\u url”:“http:\/\/Vimeo.com\/”,“title”:“清晨Qena”,“author\u name”:“Oliver Wilkins”,“author\u url”:“http:\/\/Vimeo.com\/分支”,“is\u plus”:“1”,“html”:“宽度”:1280,“高度”:720,“持续时间”:229,“描述”:“清晨在Qena附近的Quft,用EX1和Letus极限35毫米自由度适配器拍摄。\n\nww.offshoot.tv\n”,“缩略图url:“http:\/\/b.vimeocdn.com\/ts\/271\/854\/27185484\/u 640.jpg”,“缩略图宽度”:640,“缩略图高度”:360,“视频id”:2197639}”
您需要使用JSONP,因为您正在尝试执行跨域AJAX调用。看起来vimeo支持它。您只需要通过修改url来指定回调(注意我在末尾附加的回调=?
参数和格式=JSONP
):
还有一个问题。谢谢,但是你能告诉我为什么它在safari中起作用吗?我想同样的安全问题也适用于safari吗?@Wesley,是的,通常应该适用。也许你已经修改了一些设置。我不知道。你不能相信这在任何浏览器上都能起作用。使用JSONP确保你的代码在每个浏览器上都能起作用。Ac实际上,如果我看一下vimeo自己的文档:-他们甚至没有提到JSONP?@Wesley,好吧,我没有读过他们的文档。我只是用标准用于JSONP的
callback
参数测试了url,它起了作用。啊,现在我看了他们的文档,他们谈到了callback
参数。你为什么这么说嘿,别提了?callback:(可选)当返回JSON时,用这个函数包装。
这正是JSONP是什么=>在回调函数中包装JSON。oembed标准也没有提到任何关于JSONP的内容。你确定这里没有其他问题吗?(谢谢)
$.ajax({
url: "http://vimeo.com/api/oembed.json?format=jsonp&url=http%3A%2F%2Fvimeo.com%2F2197639&callback=?",
dataType: "jsonp",
success: function(data) {
$('#output').text(JSON.stringify(data));
},
error: function(errorSender, errorMsg) {
$('#output').text(errorSender + ' ' + errorMsg);
}
});