【发布时间】:2018-11-28 02:21:17
【问题描述】:
当我尝试从 asp.core api 返回响应时遇到问题 "angular 2 http get Object { _body: error, status: 0, ok: false, statusText: "", headers: {…}, type: 3, url: null }"
组件:
import { Component, OnInit } from '@angular/core';
import { IIndustryType } from '../../../model/IndustryType';
import { DeveloperJobDataService }from'../../../services/developerjob.dataservice';
@Component({
selector: 'add-job',
templateUrl: './addJob.component.html',
styleUrls: ['./addJob.component.css'],
providers: [DeveloperJobDataService]
})
export class AddJobComponent {
errorMessage: string;
private industry: IIndustryType[];
constructor(private dataService: DeveloperJobDataService) {
this.dataService.GetIndustries().subscribe(result => {
this.industry = result.json() as IIndustryType[];
console.log(this.industry);
}, error => console.error(error));
}
}
界面:
export interface IIndustryType {
id: number,
name: string,
description: string,
isActive: boolean,
createdDate: Date
}
服务:
import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { IIndustryType } from '../model/IndustryType';
import { IndustryType } from '../model/viewModel';
@Injectable()
export class DeveloperJobDataService {
private baseUrl: string = 'http://localhost:49861/api/Industries';
constructor(private http: Http) { }
public GetIndustries() {
return this.http.get(this.baseUrl);
}
}
错误:
【问题讨论】:
-
你可以在组件的 subscribe 方法中添加一个
console.log(result);吗?并给我输出 -
同样的结果.... GET localhost:49861/api/Industries 200 (OK) 响应 {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers, ...}跨度>
-
headers : Headers {_headers: Map(0), _normalizedNames: Map(0)} ok : false status : 0 statusText : "" type : 3 url : null _body : ProgressEvent {isTrusted: true, lengthComputable: false, 加载: 1, 总数: 0, type: "error", ...} proto : Body