加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – Github API和Access-Control-Allow-Origin

发布时间:2020-12-16 01:34:32 所属栏目:百科 来源:网络整理
导读:这可能是一个简单的(系列)问题,但我无法绕过它. 我正在尝试从我网站上托管的网络应用程序访问github api.这是代码简而言之: !DOCTYPE htmlhtmlhead stylep { color:red; }/style script src="http://code.jquery.com/jquery-latest.js"/script script $(doc
这可能是一个简单的(系列)问题,但我无法绕过它.

我正在尝试从我网站上托管的网络应用程序访问github api.这是代码简而言之:

<!DOCTYPE html>
<html>
<head>
  <style>p { color:red; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script>
  $(document).ready(function () {

$.ajax( {url :'https://api.github.com/repos/janesconference/kievIIPlugins/commits',dataType: "json",cache: false,success: function (data,textStatus,jqXHR)
        {
            var lastCommitSha = data[0].sha;
            $("p").text("Last commit SHA: " + lastCommitSha);
        }
    });
});
  </script>

</head>
<body>
  <p>Ajax request not (yet) executed.</p>

</body>
</html>

如果我将浏览器指向上传的这个简单页面on my dropbox account一切正常.
相反,如果我将浏览器指向上传到on my site的这个简单页面,我会得到臭名昭着的Access-Control-Allow-Origin异常:

XMLHttpRequest cannot load https://api.github.com/repos/janesconference/kievIIPlugins/commits?_=1360060011783. Origin http://bitterspring.net is not allowed by Access-Control-Allow-Origin.

所以,问题:

>为什么它适用于Dropbox?
>我理解使用CORS它甚至可以在网站上运行.这是将Access-Control-Allow-Origin:* .github.com放在我的Apache配置上或类似的问题.但是,如引自en.wiki,

However,this might not be appropriate for situations in which security is a concern

>有没有办法在不更改Apache配置的情况下执行此操作?可能,我无法触及我的托管网站的Apache conf,并且始终存在安全问题.这样做的正确方法是什么?

为了让CORS适用于您的网站(例如 http://example.com),您必须在此处创建GitHub OAuth应用程序来启用它: https://github.com/settings/applications

由于您使用GitHub应用程序来使CORS正常工作(不使用它来启用OAuth本身),您只需在“创建应用程序表单”的所有三个字段中输入您站点的URL:

>名称:http://example.com
>网址:http://example.com
>回调网址:http://example.com

请注意,如果您打算使用OAuth功能,则需要以不同方式设置回调URL.

在此之后,您应该能够从您的站点http://example.com向GitHub API发送AJAX请求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读