gpt4 book ai didi

javascript - CORS 错误 : “requests are only supported for protocol schemes: http…” etc

转载 作者:可可西里 更新时间:2023-11-01 01:45:00 25 4
gpt4 key购买 nike

我正在尝试运行一个简单的应用程序。我有一个 Express 后端,它在访问 localhost:4201/ticker 时返回一个 JSON 字符串。当我运行服务器并通过 http 从我的 Angular 服务向此链接发出请求时,我收到以下错误:

XMLHttpRequest cannot load localhost:4201/ticker. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

我阅读了以下文章:Understanding and Using 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'
};
}

getTickerData(): Observable<Response> {
return this._http.get(this._baseUrl + this._endpoints['ticker'])
.map(resp => resp.json())
}

}

以下是我如何使用我的服务:

getTicker() {
let a = new Array<{[key: string]: any}>();
this._tickerService.getTickerData()
.subscribe(
data => {
let parsed = JSON.parse(JSON.stringify(data));
Object.keys(parsed)
.forEach((k) => {
a.push({k: parsed[k]});
});
this.data = a;
},
error => console.log(error.toString()),
() => console.log('Finished fetching ticker data')
);
return this.data;
}

最佳答案

XMLHttpRequest cannot load localhost:4201/ticker. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

每当您看到“仅支持协议(protocol)方案” 消息时,几乎可以肯定这意味着您只是忘记了输入 httpshttp 在您的代码中的请求 URL 上。

所以在这种情况下,解决方法是在此处的代码中使用 URL http://localhost:4201/ticker:

this._baseUrl = 'http://localhost:4201/';

...因为没有 http://localhost:4201/ticker 并不是您真正想要的 URL。

关于javascript - CORS 错误 : “requests are only supported for protocol schemes: http…” etc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com