我正在尝试一个简单的应用程序。我有一个Express后端,在访问时返回一个JSON字符串localhost:4201/ticker。当我运行服务器并从我的Angular服务请求此链接时http,我收到以下错误:XMLHttpRequest无法加载localhost:4201 / ticker。交叉源请求仅支持协议方案:http,数据,chrome,chrome-extension,https。我阅读了以下文章:了解和使用CORS,如上所述,将cors模块与我的快速服务器一起使用。但是,我仍然得到上面给出的错误。部分代码如下:服务器代码:private constructor(baseUrl: string, port: number) { this._baseUrl = baseUrl; this._port = port; this._express = Express(); this._express.use(Cors()); this._handlers = {}; this._hInstance = new Handlers(); this.initHandlers(); this.initExpress();}private initHandlers(): void { // define all the routes here and map to handlers this._handlers['ticker'] = this._hInstance.ticker;}private initExpress(): void { Object.keys(this._handlers) .forEach((key) => { this._express.route(this._url(key)) .get(this._handlers[key]); });}private _url(k: string): string { return '/' + k;}这是处理函数:ticker(req: Request, res: Response): void { Handlers._poloniex .getTicker() .then((d) => { return Filters.tickerFilter(d, Handlers._poloniex.getBases()); }) .then((fdata) => { //res.header('Access-Control-Allow-Origin', "*"); //res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header('Content-Type', 'application/json'); res.send(JSON.stringify(fdata)); }) .catch((err) => { this._logger.log([ 'Error: ' + err.toString(), 'File: ' + 'handlers.class.ts', 'Method: ' + 'ticker' ], true); }); }这是我的Angular服务:export class LiveTickerService { private _baseUrl: string; private _endpoints: {[key:string]: string}; constructor( private _http: Http ) { this._baseUrl = 'localhost:4201/'; this._endpoints = { 'ticker': 'ticker' }; }
添加回答
举报
0/150
提交
取消