export class TopicComponent {
csvUrl = 'https://docs.google.com/spreadsheets/d/1DEhSS05msJGTV7rIZEPBjYFH_FK2G3o4okWTdi_Xxsc/edit?usp=sharing';
csvData: any [] = [];
constructor(private http: Http) { }
readCsvData() {
this.http.get(this.csvUrl)
.subscribe(
data => this.extractData(data),
err => this.handleError(err)
);
}
private extractData(res: any) {
let csvData = res['_body'] || '';
let allTextLines = csvData.split(/\r\n|\n/);
let headers = allTextLines[0].split(',');
let lines = [];
for ( let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(',');
if (data.length === headers.length) {
let tarr = [];
for ( let j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
lines.push(tarr);
}
}
this.csvData = lines;
}
private handleError (error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return errMsg;
}
&#13;
<h2>CSV data</h2>
<div>
<button class="btn btn1 btn2" (click)="readCsvData()">Read Csv Data</button>
</div>
<br/><br/>
<div>
<table border="1" class="demoTable">
<tr *ngFor = "let x of csvData">
<td *ngFor = "let y of x ">{{ y }}</td>
</tr>
</table>
</div>
&#13;
在预检响应中,Access-Control-Allow-Header不允许使用请求标头字段X-XSRF-TOKEN。
here is the image of the error encountered
请帮助!!
答案 0 :(得分:0)
有同样的问题。 在我的情况下,原因是在我的Chrome Cookie中保存了X-XSRF-TOKEN字段。不知何故,Chrome为OPTION请求添加了标题“Access-Control-Request-Headers:x-xsrf-token”。在Firefox中,同样的页面工作正常,在隐身模式Chrome中也是如此。 所以我只删除了这个cookie字段(X-XSRF-TOKEN),就是这样。