我已经在 CORS 上挣扎了很长时间,但仍然没有完全理解。
我最简单的例子是使用奇妙清单 API -
使用以下代码:
var settings = {
"async": true,
"crossDomain": true,
"url": "http://a.wunderlist.com/api/v1/lists",
"method": "GET",
"headers": {
"x-client-id": "{ID}",
"x-access-token": "{TOKEN}",
"cache-control": "no-cache"
},
"data": "{\n\t\"revision\": 1,\n\t\"completed\": true\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
在 Postman/Fiddler 内将返回结果。但是,将其扔到基本站点上,否则 Codepen 将返回 a 405、 aPre-flight Warning或 anInvalid Request
我粗略地了解到您在服务器端允许它,但我必须假设不是每个站点都允许 Postman 等连接,也不是我注册的每个供应商都允许我的域。
那么,您如何在 API 调用中绕过 CORS 合规性?我已经尝试了很多我读过的东西,包括 crossDomain、Cross-Origin Header 等,但总是得到相同的结果。
任何见解?
我正在按照一个简单的指南,轻轻地修改它以满足我的需求 - 但我终于遇到了关于mapping和的问题subscribing
https://www.sitepoint.com/mean-stack-angular-2-angular-cli/ - 这是指南,一切正常,直到我构建应用程序后构建应用程序List Service.
由于Angular 5,我已经做了一些修改,例如用HttpClient替换,但没有任何异常.
我现在的问题在于这两个功能:
public getAllTasks():Observable<Task[]> {
let URI = `${this.serverApi}/tasks/`;
return this.http.get(URI)
.map(res => res.json())
.map(res => <Task[]>res.tasks);
}
public deleteTask(taskId : string){
let URI = `${this.serverApi}/tasks/${taskId}`;
let headers = new HttpHeaders;
headers.append('Content-Type', 'application/json');
return this.http.delete(URI, {headers})
.map(res => res.json());
}
Run Code Online (Sandbox Code Playgroud)
甚至我的IDE告诉我
.map(res => res.json())
Run Code Online (Sandbox Code Playgroud)
由于这两种功能都不起作用 Property 'json' does not exist on type 'Object'.
我已经检查了一下,发现了一些较旧的帖子建议,比如将其修改为
.map((res: Response) => res.json())
Run Code Online (Sandbox Code Playgroud)
但这给了我一个非常相似的错误
A function whose declared type is neither 'void' …Run Code Online (Sandbox Code Playgroud) 我正在尝试将特殊符号({W} {B} {U}等)转换为各自的颜色,所以我写了一个案例,但我认为一个案例不是我需要的,因为它一旦找到它就会结束一场比赛.
print 'Test a Color'
color = gets.chomp
case color
when '{W}'
puts 'White'
when '{R}'
puts 'Red'
when '{G}'
puts 'Green'
when '{U}'
puts 'Blue'
when '{B}'
puts 'Black'
end
Run Code Online (Sandbox Code Playgroud)
{B}让我变黑,{U}得到我的蓝色.{U} {B}崩溃/什么都不返回.我怎么会让它继续下去?
我正在使用Auth0的API来处理用户管理.一切都按预期工作 - Login()正确存储本地.在Logout()它上面正确删除它们.
但是,实际Login按钮不会Logout自动成功 - 这是我硬刷页面时,而不是直接刷新页面.我相信这是我绑定的问题?
标题组件
authenticated: boolean; // Wasn't sure how else to filter in front-end
constructor(private authService: AuthService) { }
ngOnInit() {
this.authenticated = this.authService.isAuthenticated()
}
login() {
this.authService.login();
}
logout() {
this.authService.logout();
}
}
Run Code Online (Sandbox Code Playgroud)
用于标题的HTML
<li class="nav-item">
<a class="nav-link waves-light" *ngIf="!authenticated" mdbRippleRadius (click)="login()"><i class="fa fa-user"></i> <span class="clearfix d-none d-sm-inline-block">Log In</span></a>
<a class="nav-link waves-light" *ngIf="authenticated" mdbRippleRadius (click)="logout()"><i class="fa fa-user"></i> <span class="clearfix d-none d-sm-inline-block">Log Out</span></a>
</li>
Run Code Online (Sandbox Code Playgroud)
文档说使用auth.isAuthenticated()哪个将从服务调用该函数
public isAuthenticated(): …Run Code Online (Sandbox Code Playgroud)