【发布时间】:2018-06-07 16:25:44
【问题描述】:
我是新手,如果我使用了错误的术语,我深表歉意。我正在尝试使用 Trello API 提取数据,但在 Chrome 控制台中收到以下错误:
无法加载https://api.trello.com/1/cards/5a42e19364345a7d84ba3f5f/members:当请求的凭据模式为“包含”时,响应中的“Access-Control-Allow-Origin”标头的值不能是通配符“*”。因此,Origin 'http://localhost:8080' 不允许访问。 XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
在做了一些研究后,我发现这是一个 CORS 问题。我正在使用带有 Python 的 Google App Engine。这个错误是我可以修复的还是 API 的错误?我已经设法使用此 API 发出 POST 请求,这没问题。我已经阅读了很多关于 CORS 的信息,但还没有找到解决问题的方法。
这是我的 GET 请求的 Javascript 代码,它只是从 Trello API 复制/粘贴的,所以我不确定出了什么问题:
var authenticationSuccess = function() {
console.log('Successful authentication');
};
var authenticationFailure = function() {
console.log('Failed authentication');
};
window.Trello.authorize({
type: 'popup',
name: 'Work Requests App',
scope: {
read: 'true',
write: 'true' },
expiration: 'never',
success: authenticationSuccess,
error: authenticationFailure
});
var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("GET", "https://api.trello.com/1/cards/5a42e1936434a7d84ba3f5f/members");
xhr.send(data);
【问题讨论】:
-
你为什么使用
withCredentials = true?你知道what it does吗? -
是的。如果我将其设置为 false,则不会进行身份验证。另外,您到底为什么将这个问题标记为重复?我没有使用 Java/Spring/Angular/SockJS,这些答案应该如何帮助我?没有任何答案适用,并且该问题仅发生在 Chrome 中的 OP 上。
-
重新打开。没想到这么不一样
-
你为什么不使用
client.jsAPI to perform the requests?大概它在授权后存储所需的凭据并将它们作为参数/标头添加到请求中。另外,我想说您需要在授权成功后执行这些操作(即在authenticationSuccess回调中) -
我为你提出了一个 API 文档错误 ~ github.com/trello/api-docs/issues/150
标签: javascript cors trello