【发布时间】:2019-12-16 12:46:29
【问题描述】:
我正在尝试以角度从资产文件夹中读取 XML 文件,我尝试了 HttpClient get 方法来获取数据,但它显示以下错误,我在 http 方法中传递了交叉标头。
请告诉我如何在 Angular 中读取 XML 文件
错误:
http 失败响应(未知 url):0 未知错误”,错误:错误
代码:
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
constructor(private router: Router, private httpClient: HttpClient) {
this.getProducts();
}
getProducts() {
const headers = new HttpHeaders(
{ 'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Origin': '*'
});
headers.set('Content-Type', 'text/xml');
this.httpClient
.get('assets/test.xml', { headers, responseType: 'text' })
.subscribe(output => console.log(output));
}
}
【问题讨论】:
标签: angular