CORS Angular 2 拦截器
CORS Angular 2 interceptor
当我请求 "api/authenticate" 或 "api/account" 时,拦截器将我发送到 http://localhost:8443 (http://localhost:8443/api/authenticate for example), but in other cases ("api/plan-reports" for example) i need to ask to http://localhost:8443/hermessm/api/plan-reports .
所以我有以下错误。
有人知道怎么解决吗?
在我看来是拦截器的错误。请帮帮我!
interceptor.ts
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.request(url, this.getRequestOptionArgs(options)));
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.get(url, this.getRequestOptionArgs(options));
}
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.post(url, body, this.getRequestOptionArgs(options));
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.put(url, body, this.getRequestOptionArgs(options));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.delete(url, this.getRequestOptionArgs(options));
}
getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
if (!options) {
options = new RequestOptions();
}
if (!options.headers) {
options.headers = new Headers();
}
options.withCredentials = true;
return !this.firstInterceptor ? options : this.firstInterceptor.processRequestInterception(options);
}
intercept(observable: Observable<Response>): Observable<Response> {
return !this.firstInterceptor ? observable : this.firstInterceptor.processResponseInterception(observable);
}
private updateUrl(req: string) {
if (req.indexOf("api/authenticate") !== -1 || req.indexOf("api/account") !== -1) {
console.log(environment.gataway + '/' + req)
return environment.gataway + '/' + req;
}
else if (req.indexOf("api/") !== -1) {
return environment.gataway + '/hermessm/' + req;
}
else {
return req;
}
}
}
您似乎在使用网络 API
现在浏览器不允许从其他域访问服务,这就是您收到此错误的原因。
您需要安装 CORS,因此 运行 在您的包管理器控制台上执行以下命令:
Install-Package Microsoft.AspNet.WebApi.Cors
将以下行添加到您的 webapiconfig.cs 文件
var cors = new EnableCorsAttribute("http://localhost:xxx", "*", "*");
config.EnableCors(cors);
现在你可以开始了
原因是我的拦截器。我没有解决这个问题,但我删除了拦截器并将代理服务器添加到我的项目
当我请求 "api/authenticate" 或 "api/account" 时,拦截器将我发送到 http://localhost:8443 (http://localhost:8443/api/authenticate for example), but in other cases ("api/plan-reports" for example) i need to ask to http://localhost:8443/hermessm/api/plan-reports .
所以我有以下错误。
有人知道怎么解决吗? 在我看来是拦截器的错误。请帮帮我!
interceptor.ts
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.request(url, this.getRequestOptionArgs(options)));
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.get(url, this.getRequestOptionArgs(options));
}
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.post(url, body, this.getRequestOptionArgs(options));
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.put(url, body, this.getRequestOptionArgs(options));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.delete(url, this.getRequestOptionArgs(options));
}
getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
if (!options) {
options = new RequestOptions();
}
if (!options.headers) {
options.headers = new Headers();
}
options.withCredentials = true;
return !this.firstInterceptor ? options : this.firstInterceptor.processRequestInterception(options);
}
intercept(observable: Observable<Response>): Observable<Response> {
return !this.firstInterceptor ? observable : this.firstInterceptor.processResponseInterception(observable);
}
private updateUrl(req: string) {
if (req.indexOf("api/authenticate") !== -1 || req.indexOf("api/account") !== -1) {
console.log(environment.gataway + '/' + req)
return environment.gataway + '/' + req;
}
else if (req.indexOf("api/") !== -1) {
return environment.gataway + '/hermessm/' + req;
}
else {
return req;
}
}
}
您似乎在使用网络 API
现在浏览器不允许从其他域访问服务,这就是您收到此错误的原因。
您需要安装 CORS,因此 运行 在您的包管理器控制台上执行以下命令:
Install-Package Microsoft.AspNet.WebApi.Cors
将以下行添加到您的 webapiconfig.cs 文件
var cors = new EnableCorsAttribute("http://localhost:xxx", "*", "*");
config.EnableCors(cors);
现在你可以开始了
原因是我的拦截器。我没有解决这个问题,但我删除了拦截器并将代理服务器添加到我的项目