小编典典

请求的资源错误中不存在“ Access-Control-Allow-Origin”标头

ajax

我正在尝试获取新闻网站的供稿。以为我会使用Google的feed
API将feedburner供稿转换为json。以下网址将以json格式从Feed中返回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
”。

我该如何解决?


阅读 214

收藏
2020-07-26

共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,则调用将成功。

2020-07-26