相关疑难解决方法(0)

Angular 4:如何使用HTTPClient读取文本文件的内容

我的Angular 4项目目录中有一个.txt文件,我想阅读其内容.怎么做 ?以下是我使用的代码.

该文件位于"app"文件夹中的"files"文件夹中.我有HTTPClient代码的组件位于'httpclient'文件夹中,该文件夹位于'app'文件夹中.

含义'files'文件夹和'httpclient'文件夹是子文件夹.

代码如下所示.它没有工作因为我得到404错误 - 'GET http:// localhost:4200/files/1.txt 404(Not Found)'

this.http.get('/files/1.txt').subscribe(data => {
        console.log(data);
    },
        (err: HttpErrorResponse) => {
            if (err.error instanceof Error) {
                // A client-side or network error occurred. Handle it accordingly.
                console.log('An error occurred:', err.error.message);
            } else {
                // The backend returned an unsuccessful response code.
                // The response body may contain clues as to what went wrong,
                console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
            }
        }
    );
Run Code Online (Sandbox Code Playgroud)

javascript angular

11
推荐指数
2
解决办法
4万
查看次数

Angular 关闭 json 解析器

服务器以纯文本响应并尝试解析 json。如何在一个组件中转变这种行为?

get(mrn: string): Observable<string> {
    let url: string;
    url = this.Url;
    url = url + '?mrn=' + mrn;
    return this.http.get<string>(url);
  }


download(testobject: Testobject): void {
    this.httpService.get(testobject.mrn)
      .subscribe(ccresult => {
        console.log('ccresult ist: ' + ccresult);
      });
  }
Run Code Online (Sandbox Code Playgroud)

错误:错误:SyntaxError:JSON 中的意外标记 N 在 XMLHttp 的 JSON.parse () 的位置 0 处......

json angular

0
推荐指数
1
解决办法
2687
查看次数

标签 统计

angular ×2

javascript ×1

json ×1