提问者:小点点

请求的资源错误上没有“access-control-allow-origin”标头


我正在尝试获取一个新闻网站的提要。我想我应该使用Google的feed API将feedburner提要转换为JSON。下面的url将以json格式从提要返回10个帖子。http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/Mathrubhumi

我使用下面的代码来获取上面url的内容

$.ajax({
  type: "GET",
  dataType: "jsonp",
  url: "http://ajax.googleapis.com/ajax/services/feed/load",
  data: {
    "v": "1.0",
    "num": "10",
    "q": "http://feeds.feedburner.com/mathrubhumi"
  },
  success: function(result) {
    //.....
  }
});

但它不起作用,我得到以下错误

XMLHttpRequest无法加载http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http%3a%2f%2ffeeds.feedburner.com%2fMathrubhumi。请求的资源上没有“access-control-allow-origin”标头。因此,不允许访问源“http://localhost”。

我怎么解决这个?


共1个答案

匿名用户

我认为这可能是Chrome不支持localhost来查看access-control-allow-origin--请参阅Chrome问题

要让Chrome在头中发送access-control-allow-origin,只需将/etc/hosts文件中的localhost别名到其他域,如:

127.0.0.1   localhost yourdomain.com

那么,如果您使用yourdomain.com而不是localhost访问脚本,则调用应该会成功。