Ajax请求适用于safari,但不适用于Firefox(Vimeo oembed)

时间:2011-09-23 15:55:16

标签: jquery ajax json firefox oembed

我正在尝试通过oembed API(json)获取vimeo嵌入代码。

它在safari中运行良好,但在Firefox中,似乎返回的json没有被正确解释,因为我得到一个空值而不是javascript对象(在success方法中)。

我给了一个jsfiddle示例的链接,但样本在那里不起作用,关于不允许的原点的一些错误..

所以这是代码:

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

1 个答案:

答案 0 :(得分:3)

您需要使用JSONP,因为您正在尝试执行跨域AJAX调用。它看起来vimeo支持它。您只需要通过修改网址来指定回调(请注意我在末尾添加的callback=?参数和format=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);
    }
});

这是一个live demo