【问题标题】:Posting to YouTube API with jQuery使用 jQuery 发布到 YouTube API
【发布时间】:2010-08-31 16:44:20
【问题描述】:

我正在尝试向 google authSub 发布请求以获取 youtube。我将 jQuery 用于 AJAX 帖子等。当我发布 POST 时出现 405 错误:“405 Method Not Allowed”。

这是我的 js:

   $.ajax({
      type: 'POST',
      url: "https://www.google.com/accounts/AuthSubRequest",
      beforeSend: function(xhr){
         xhr.setRequestHeader('X-GData-Key', 'key="' + ytKey + '"');
      },
      success: function(oData){
         //alert(oData);
      },
      scope: 'http://gdata.youtube.com',
      alt: 'json',
      next: 'http://' + globalBrand + '/sitecreator/view.do',
      session: 1
   });

我在 API 中为此使用的页面是 here

这是错误的样子:

【问题讨论】:

  • 您能否提供您正在使用的 HTML 和 JS 的链接?我正在尝试完成同样的事情,但我不确定如何实现 ajax js。

标签: javascript ajax jquery youtube-api


【解决方案1】:

您的请求数据参数放错了位置,见下文:

$.ajax({
      type: 'POST',
      url: "https://www.google.com/accounts/AuthSubRequest",
      data: {
        scope: 'http://gdata.youtube.com',
        alt: 'json',
        next: 'http://' + globalBrand + '/sitecreator/view.do',
        session: 1
      },
      beforeSend: function(xhr){
         xhr.setRequestHeader('X-GData-Key', 'key="' + ytKey + '"');
      },
      success: function(oData){
         //alert(oData);
      }

   });

现在可能是你做错了什么,但这肯定需要纠正。

【讨论】:

  • 啊,哇,真不敢相信我错过了那个。但是,我仍然收到 405 错误。
  • 你想用alt: 'json'做什么我在你发布的 API 链接中没有看到。看起来您还缺少Authorization: AuthSub token="<authentication_token>" 的标题
  • 我刚刚补充说,出于记忆的目的,我将提出其他请求。我现在把它注释掉了,我仍然得到 405。
  • firebug 中的请求是什么样子的......您应该可以检查标头是否正确以及数据是否正确编码。
  • 我在我原来的 firebug 问题中添加了一张图片,标题为 405 错误。
【解决方案2】:

啊,这是解决问题的方法。如果我使用构建的 url 发出请求并将其作为 href 分配给锚点或在 window.open() 中调用它;它有效。

window.open('https://www.google.com/accounts/AuthSubRequest?scope=http://gdata.youtube.com&next=http://' + globalBrand + '/sitecreator/view.do&session=1');

至于为什么 jQuery 的 ajax 方法被拒绝我不知道。这似乎也是一个问题elsewhere

【讨论】:

    【解决方案3】:

    JQuery中HTTP HEADER OPTIONS报错的解决方法,这个请求对我来说没问题:

    var word = 'search+word';
    $.ajax({
            type: "GET",
            url: "http://gdata.youtube.com/feeds/api/videos?q="+word+"&max-results=10&alt=json-in-script&format=5,
            cache: false,
            dataType:'jsonp',
            success: function(data){
                    //json 'data' var treatment
            },
           error: function(XMLHttpRequest, textStatus, errorThrown, data){
                alert("Not able to fetch the data due to feed unavailability!!!");
            }
    });
    

    参考:http://www.mohammedarif.com/?p=180

    【讨论】:

    • 感谢您成为我的推荐人 renedet :)
    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 2015-09-13
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多