gpt4 book ai didi

javascript - Angular 2 访问 redmine Rest Api,无 'Access-Control-Allow-Origin'

转载 作者:行者123 更新时间:2023-12-01 03:39:49 25 4
gpt4 key购买 nike

我尝试通过他们的 Rest Api 从 redmine 获取问题。当我从 postman 处调用它时,我得到响应,但是当我从我的 Angular 应用程序调用它时,我收到这样的错误

OPTIONS https://redmine.ourDomain.net/issues.json 404 (Not Found) XMLHttpRequest cannot load https://redmine.ourDomain.net/issues.json. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.

这就是我在 Angular 中的做法

login(user: User): Observable<boolean> {
var headers: Headers = new Headers();
headers.append("Authorization", "Basic " + btoa(user.login + ":" + user.password));
let options = new RequestOptions({ headers: headers });
return this.http.get("https://redmine.ourDomain.net/issues.json", options)
.map((response: Response) => {
debugger;
if (response.status == 200) {
// set token property

// store username and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify({ user }));

// return true to indicate successful login
return true;
} else {
// return false to indicate failed login
return false;
}
});
}

请求在我的浏览器中看起来是这样的 enter image description here

最佳答案

您需要在后端启用 CORS 访问:http://www.redmine.org/plugins/redmine_cors

这是一个很好的扩展,可以让您在正常的 CORS 限制之外测试前端代码。它严格用于测试,对生产应用程序没有帮助,但很高兴拥有:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

关于javascript - Angular 2 访问 redmine Rest Api,无 'Access-Control-Allow-Origin',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com