请求的资源错误中不存在“Access-Control-Allow-Origin"标头 [英] No 'Access-Control-Allow-Origin' header is present on the requested resource error

查看:60
本文介绍了请求的资源错误中不存在“Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取新闻网站的提要.以为我会使用谷歌的提要 API 将提要燃烧器提要转换为 json.以下 url 将从提要中以 json 格式返回 10 个帖子.http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/mathrubhumi

I'm trying to fetch the feed of a news website. Thought I'd use google's feed API to convert the feedburner feed into json. The following url will return 10 posts from the feed, in json format. http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http://feeds.feedburner.com/mathrubhumi

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

I used the following code to get the contents of above 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) {
    //.....
  }
});

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

but it's not working and I'm getting the following error

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'.

XMLHttpRequest cannot load http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=http%3A%2F%2Ffeeds.feedburner.com%2Fmathrubhumi. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

我该如何解决这个问题?

How do I fix this?

推荐答案

我相信这可能是 Chrome 不支持 localhost 去通过 Access-Control-Allow-Origin -- 查看 Chrome 问题

I believe this might likely be that Chrome does not support localhost to go through the Access-Control-Allow-Origin -- see Chrome issue

要让 Chrome 在标头中发送 Access-Control-Allow-Origin,只需将/etc/hosts 文件中的本地主机别名为其他域,例如:

To have Chrome send Access-Control-Allow-Origin in the header, just alias your localhost in your /etc/hosts file to some other domain, like:

127.0.0.1   localhost yourdomain.com

然后,如果您使用 yourdomain.com 而不是 localhost 访问您的脚本,调用应该会成功.

Then if you'd access your script using yourdomain.com instead of localhost, the call should succeed.

这篇关于请求的资源错误中不存在“Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆