以下Angular2 JobService实现返回错误:
job.service.ts:26发生错误响应{_body:对象,状态:404,ok:false,statusText:"未找到",标题:标题...}
当通过浏览器地址栏直接提交时,服务中定义的jobsUrl
变量返回作业结果的json结果。知道问题可能是什么?
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { JobsResult } from './jobs-result';
@Injectable()
export class JobService {
private headers = new Headers({'Content-Type': 'application/json'});
private jobsUrl = 'http://service.dice.com/api/rest/jobsearch/v1/simple.json?text=javascript';
//private jobsUrl = 'app/jobs'; // URL to web api
constructor(private http: Http) { }
getJobs(): Promise<JobsResult>{
return this.http.get(this.jobsUrl)
.toPromise()
.then(response => response.json().data as JobsResult)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('getJobs() not working');
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
}
答案 0 :(得分:1)
数据未定义,响应没有数据作为键
.then(response => response.json() as JobsResult)
而不是
.then(response => response.json().data as JobsResult)