我一直在关注角度2的视频教程 http://courses.angularclass.com/courses/enrolled/73288
当我运行api时出现上述错误。
以下是根据浏览器具有未解析参数的服务
`
import {Injectable} from '@angular/core';
import { ApiService } from './api';
export class NoteService{
path: string= '/notes';
constructor(private api: ApiService) {}
createNote(note){
return this.api.post(this.path, note);
}
getNotes(){
return this.api.get(this.path);
}
completeNote(note){
return this.api.delete(`${this.path}/${note.id}`);
}
}`
这是网页的api服务,
`
import {Injectable} from '@angular/core';
import {Headers, Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/observable/throw';
@Injectable()
export class ApiService{
headers: Headers=new Headers({
'Cotent-Type': 'application/json',
Accept: 'application/json'
});
api_url: string = 'http://localhost:3500';
constructor(private http: Http){
}
private getJson(resp: Response){
return resp.json();
}
private checkForError(resp: Response): Response{
if(resp.status >=200 && resp.status<300){
return resp;
}
else{
const error=new Error(resp.statusText);
error['response']= resp;
console.error(error);
throw error;
}
}
get(path: string): Observable<any>{
return this.http.get(`${this.api_url}${path}`, this.headers)
.map(this.checkForError)
.catch(err=> Observable.throw(err))
.map(this.getJson)
}
post(path: string, body): Observable<any>{
return this.http.post(
`${this.api_url}${path}`,
JSON.stringify(body),
{headers: this.headers}
)
.map(this.checkForError)
.catch(err=> Observable.throw(err))
.map(this.getJson);
}
delete(path: string): Observable<any>{
return this.http.delete(`${this.api_url}${path}`, this.headers)
.map(this.checkForError)
.catch(err=> Observable.throw(err))
.map(this.getJson)
}
}
`
如果问题中应包含任何其他文件,请在评论中提及。 谢谢。
答案 0 :(得分:0)
@Injectable() // required when a service has constructor parameters
export class NoteService{