편집 : 질문 끝 부분을 읽으십시오!TypeError : error.json이 함수가 아닙니다.
내 서비스 코드 :
import { Http, Response, Headers } from "@angular/http";
import { Injectable } from "@angular/core";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Observable } from "rxjs";
import { Client } from "./client.model";
@Injectable()
export class ClientService {
private clients: Client[] = [];
constructor(private http: Http){}
addClient(client: Client) {
this.clients.push(client);
const body = JSON.stringify(client);
const headers = new Headers({'Content-Type': 'application/json'});
return this.http.post('http://localhost:3000/client', body, {headers: headers})
.map((response: Response) => response.json())
.catch((error: Response) => Observable.throw(error.json()));
}
getClients() {
return this.clients;
}
deleteClient(client: Client) {
}
}
그리고 내 구성 요소에 내가 가진
이 제출 기능 :onSubmit(form: NgForm) {
let email = form.value.email;
let password = form.value.password;
let firstName = form.value.firstName;
let lastName = form.value.lastName;
const client = new Client(email, password, firstName, lastName);
this.clientService.addClient(client)
.subscribe(
data => console.log(data),
error => console.error(error)
);
form.resetForm();
}
유사한 오류가 here 및 here. 그 대답은 항상 rxjs 함수를 포함하는 것이지만, 나는 그렇게합니다. 그래서 나는 확실히 확신 할 수 없습니다. 왜이 에러가 나옵니까.
편집 :
그래서 실제 문제는이 기능이 아니었지만, 누락 "/"주 app.js 파일에 내 경로 전에. 이를 해결 한 후에 문제는 사라졌습니다.
가능한 중복 : http://stackoverflow.com/questions/39162056/angular2-res-json-is-not- ([Angular2는 res.json는 함수가 아닙니다] a-function) – Igor
@Igor 당신은 내 구멍 질문을 읽었습니까, 그렇죠? 나는 비슷한 문제가 있다는 것을 지적했다. 그것은 나를 위해 일하지 않는 것 같다 .. – Zoker
나는 당신의 전체적인 질문을 읽었다. 그 이유는 복제물에 json을 포함하지 않기 때문입니다. 같은 범인을 가진 다른 유사한 질문이 있습니다. 브라우저에서 디버그하고 오류의 응답 상태를 확인하면 실제로 json을 사용할 수 없다는 것을 알게 될 것입니다. 또한 브라우저 디버거의 네트워크 IO 탭에서 결과를 확인하여이 작업을 수행 할 수 있습니다. – Igor