연락처 2에서 연락처를 처리하는 서비스를 만들려고합니다. 이것은 내가 지금까지 얻은 것입니다. 내가 응답에 400+
상태 코드를 얻을 경우각도 2 약속 던짐 오류
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class ContactsService {
constructor(private http: Http) { }
addContact(contact): Promise<any> {
return this.http
.post('http://localhost:8000/contacts', contact)
.toPromise()
.then(response => response.json())
.catch(error => error.json());
}
}
지금은 서비스가 잘 작동 그것이 then
상태로 전환 200
코드 그리고 응답을 반환하는 경우, 코드는 catch
응시로 이동합니다.
하지만 컴포넌트 내부에서 사용할 때 respone이 괜찮은지 여부와 상관없이 then
상태가됩니다. 코드가 나는 400+
상태 코드를 얻을 수 넣다 시작 오류로 이동 있도록
addingContact() {
this.contactsService
.addContact(this.user)
.then(
(contactx) => { console.log('THEN = ' + JSON.stringify(contactx)); },
(err) => { console.log('CATCH = ' + JSON.stringify(err)); }
);
}
내가 서비스에 뭔가를 던져해야 내가 부족 뭔가가 있나요?
미리 감사드립니다. Daniel!
실제로 위의 코드는 내 문제를 해결했습니다. :). 고맙습니다! – Devner