0

我收到“Access-Control-Allow-Origin 不允许”错误。我应该如何使用 API?我得到了代码,但令牌的 POST 请求失败

var gist = {

clientId: 'clientId',
clientSecret: 'clientSecret',
authUrl: 'https://github.com/login/oauth/authorize',
tokenUrl: 'https://github.com/login/oauth/access_token',

getCode: function() {
    var url = gist.authUrl+'?client_id='+gist.clientId;
    location.href = url;
},
processCode: function() {
    var code = location.search.slice(6);
    var url = gist.tokenUrl;
    $.post(gist.tokenUrl, {
        client_id : gist.clientId,
        client_secret : gist.clientSecret,
        code : code
    }, function(){
        console.log("POST request sent");
    })
        .success(function(data){
            console.log(data);
        })
        .error(function(data){
            console.error("POST request error");
        })
}
}
4

1 回答 1

0

根据文档:http: //developer.github.com/v3/#cross-origin-resource-sharing

Any domain that is registered as an OAuth Application is accepted.

要注册您的应用程序,请访问:https ://github.com/settings/applications

您需要从您的应用程序注册的同一个域发布。如果您尝试在本地进行测试,您可能需要修改主机文件并在端口 80 上运行服务器。

于 2012-05-17T18:42:56.327 回答