Dictionary 角度6:无';访问控制允许原点';请求的资源上存在标头
我在做什么 面临一些问题。 目前我正在使用:Dictionary 角度6:无';访问控制允许原点';请求的资源上存在标头,dictionary,rxjs,http-get,angular6,Dictionary,Rxjs,Http Get,Angular6,我在做什么 面临一些问题。 目前我正在使用: Angular CLI: 6.0.8 Node: 10.4.1 OS: linux x64 Angular: 6.0.6 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router Package
Angular CLI: 6.0.8
Node: 10.4.1
OS: linux x64
Angular: 6.0.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/cli 6.0.8
@ngtools/webpack 6.0.8
@schematics/angular 0.6.8
@schematics/update 0.6.8
rxjs 6.2.1
typescript 2.7.2
webpack 4.8.3
在,weather.service.ts中,如果我想使用
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import 'rxjs/add/operator/map';
@Injectable({
providedIn: 'root'
})
export class WeatherService {
constructor(private _http: HttpClient) { }
dailyForecast() {
return this._http.get("http://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22").map(result => result);
}
}
但这给了我一个错误:
(浏览器控制台) 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”
(终点站) ./src/app/weather.service.ts中出错 未找到模块:错误:无法解析“/home/nodira/AngularProjects/charts/src/app”中的“rxjs/add/operator/map”
我尝试了以下解决方案,但放弃了: 现在我并没有在终端中得到任何错误,但在浏览器的控制台中也并没有看到任何需要的错误。 当我想使用
filter
时,我有类似的无法解决rxjs/add/operator/filter
错误。我非常感谢你的帮助。
事先谢谢。我找到了上面提供的错误消息的一些原因。
rxjs
出错的原因是错误地声明了map函数。此链接包含有关声明更改的一些信息:
http请求
中没有得到结果的原因。我想可能是因为http://samples.openweathermap.org
blockedhttp://localhost:4200/
用于http请求
如get
似乎我找到了一个解决方案,只要项目可行。当我添加代理后端时,http问题得到了解决。有关代理后端的更多详细信息,请检查:。如果有人面临相同的问题,请在此共享解决方案: 在与
package.json
相同的级别添加一个proxy.conf.json文件。应该是这样的:
{
"/api": {
"target": "http://samples.openweatherm...",
"secure": false,
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true
}
}
在服务中更新此信息
dailyForecast() {
return this._http.get("/api/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22")
.map(result => result);
}
服从这个命令
ng-serve--proxy-config=proxy.conf.json
请随意查看提供这些问题的project代码:)在此中,问题不是来自应用程序客户端,如果您想最终解决它,您必须在后端解决它您希望执行实际console.log()的代码在哪里?它在app.component:构造函数中(private _weather:WeatherService){}ngOnInit(){this._weather.dailyForecast().subscribe(res=>{console.log(res);}
我正在将weather.service注入app.com。我认为这应该是一个评论而不是一个答案。@BlueLike谢谢你的回答。我理解你的意思。但是,我上面提到的网站的后端不在我的权限范围内。因此,我无法像你所说的那样从后端修复它。你的后端语言是java??proxy config proxy.conf.json不适用于生产
dailyForecast() {
return this._http.get("/api/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22")
.map(result => result);
}