Angular HttpClient给出错误为“Backend返回代码未定义,消息为:无法读取未定义的属性'toLowerCase'”

时间:2017-12-28 11:54:33

标签: angular angular5

我有一个component.ts文件,如下所示。

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import 'rxjs/add/operator/retry';
@Component({
  selector: 'app-add-candidate-emp-referral',
  templateUrl: './add-candidate-emp-referral.component.html',
  styleUrls: ['./add-candidate-emp-referral.component.css']
})
export class addCandidateComponent implements ngOnInit{
url: 'http://localhost:59055/users1/1';
  posts: any;
  constructor(private http: HttpClient) {
  }
  ngOnInit() {
    // this.http.get(this.url).subscribe(res => { this.posts.username = res.toString(); console.log(this.posts); console.log('hi'); });
    this.http.get(this.url).retry(3).subscribe(data => { console.log(data); console.log('hi'); }, (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},message was: ${err.message} `);
      }
    }
    );
  }
}

现在当我渲染该组件时,我无法从api url获取详细信息。但与此同时,当我使用邮递员客户端并发送网址请求时,我收到了json的响应。

1 个答案:

答案 0 :(得分:0)

这可能归因于以下几点:

  1. CORS-从同一服务器请求数据时需要特别注意,如果是这种情况,请尝试使用相对URL
  2. 服务器可能需要身份验证,是这种情况吗?
  3. 您可以共享来自上面打印的输出吗?
  4. 标题中的输出来自哪里?