【问题标题】:403 Forbidden in Google Chrome (only) when I call twitter search with Jquery (ajax)403 Forbidden in Google Chrome (only) 当我用 Jquery (ajax) 调用 twitter 搜索时
【发布时间】:2013-04-11 02:15:20
【问题描述】:

我在使用 Google Chrome (v26.0.1410.64m) 时遇到了这个问题。 当我使用 jquery 调用 Twitter 搜索时,出现以下错误:

GET https://search.twitter.com/search.json?q=youtube&rpp=5&result_type=recent&callback=jQuery19101530768966767937_1365645647773&_=1365645647774 403 (Forbidden)

这是我使用的代码:

$.ajax({
   type: "GET",
   url: "https://search.twitter.com/search.json?q=youtube&rpp=5&result_type=recent",
   contentType: "application/json",
   dataType: "jsonp",
   success: function(data) {
       console.log(data);
   }
});

在 Firefox、Internet Explorer、Safari 和 Opera 上可以正常工作。但是在 Chrome 中我有这个禁止的错误。我尝试在我的 .htaccess 中添加以下跨域代码:

Header set Access-Control-Allow-Origin: *
Header set Access-Control-Allow-Credentials: true

什么都没有改变。我哪里错了? 如果这有帮助,我正在使用 Windows/Apache 上的虚拟域(en.domain.com.local、fr.domain.com.local 等)在开发环境中工作。

感谢您的帮助。对不起我的英语。

【问题讨论】:

  • 在 Chrome 中工作,至少在这个 fiddle 中。
  • 确实.. 所以这是我的 Apache 配置?
  • 假设您是从 HTML 代码调用它,您的 Apache 和 .htaccess 配置应该与此无关。
  • 好吧,这很奇怪。我哪里错了?您需要有关某事的更多信息吗?感谢您的帮助。

标签: javascript ajax json


【解决方案1】:

好吧,我发现出了什么问题。 当我将 https 替换为 http 时,Google Chrome 已接受该查询。我猜这是一个安全问题。目前,我的网站无法使用 https 协议。谢谢!

编辑:嗯...误报。它工作过一次。但是当我刷新页面时,禁止的错误又回来了。

编辑 2: 哇...事实上,当 Google Chrome 的开发者控制台关闭时,脚本运行良好...我与 Perception 制作的小提琴有相同的结果/p>

【讨论】:

  • 刷新页面时,还是https链接到twitter吗?
  • 是的,但是当我使用谷歌浏览器的“强制缓存刷新”模式的时候,禁止的还是在这里,http或者https。
  • 这与 Google Chrome 用户配置文件有关。我有完全相同的问题:只有当开发工具在谷歌浏览器中为一个特定的谷歌浏览器用户配置文件(右上角图标)打开时,从 twitter 获取 403,但在切换到另一个时工作。
【解决方案2】:

我最近遇到了这个问题,但我找到了破解/解决方法。

我发现有效的是引入了大约 100 毫秒的任意超时。我使用了 150 毫秒来确定破解是否有效。

function latestTweet(screenName) {
  var deferred = new $.Deferred();

  // Introduce a timeout before we make a search request to Twitter.
  // This is added in the event that this function is called when
  // the page is ready and your user is using Chrome. When in Chrome
  // and a search request is sent to Twitter it will always return a 403.
  setTimeout(function () {
    $.ajax({
      url: "//search.twitter.com/search.json?q=from%3A"+screenName,
      type: 'GET',
      dataType: 'jsonp',
      success: function(data) {
        if (data && data.results) {
          data = data.results;
          if (typeof data.length === 'number') {
            deferred.resolve(data[0]);
          } else {
            deferred.reject();
          }
        }
      }
    });
  }, 150); // This works with 100ms, but pad just to be sure.

  return deferred.promise();
}

// Somewhere in my code ...
latestTweet('someScreenName').done(displayTweet);

现在在 Chrome 中您应该不会收到 403 Forbidden 响应,并且在所有其他浏览器中它会继续工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2013-06-04
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    相关资源
    最近更新 更多