我正在嘗試一個簡單的應用程序。我有一個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' }; }
CORS錯誤:“請求僅支持協議方案:http ...”等
慕絲7291255
2019-09-03 17:19:59
