मैं angular2 एप्लिकेशन पर काम कर रहा हूँ जिसमें मैं नीचे के रूप में HTTP के माध्यम से एक आराम कॉल कर रहा हूँ:कोणीय त्रुटि में मैन्युअल रूप से देखने योग्य त्रुटि कैसे फेंकें?
login(email, password) {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let body = `identity=${email}&password=${password}`;
return this.http.post(`${this._configService.getBaseUrl()}/login`, body, options)
.map((res: any) => {
let response: any = JSON.parse(res._body);
if (response.success == 0) {
Observable.throw(response); // not working
} else if (response.success == 1) {
console.log('success');
localStorage.setItem('auth_token', 'authenticated');
this.loggedIn = true;
return response;
}
});
}
मूल रूप से मैं अपने घटक मेरी सदस्यता के कॉल में प्रतिक्रिया & त्रुटि प्राप्त करना चाहते हैं।
अर्थात
this._authenticateService.login(this.loginObj['identity'],this.loginObj['password']).subscribe(
(success)=>{
this.credentialsError=null;
this.loginObj={};
this._router.navigate(['dashboard']);
},
(error)=>{
console.log(error);
this.credentialsError=error;
}
);
लेकिन मेरे एपीआई हमेशा सफलता देता है के रूप में यह आईडी कि जिस तरह से परिभाषित किया।
तो मैं जानना चाहता हूं कि response.success == 0
पर त्रुटि संदेश कैसे फेंक सकता है, ताकि इसे मेरे सब्सक्राइब कॉलबैक के त्रुटि तर्क के अंदर पहुंचाया जा सके।
क्या है स्थिति कोड आप मिल रहा है, मेरा मतलब यह किसी और फिर 0 हो सकता है? –
@MrJSingh जिस व्यक्ति ने एपीआई डिज़ाइन किया है, केवल प्रतिक्रिया को सत्यापित करने के लिए 0 या 1 के रूप में सफलता लौटा दी। इसलिए मैं अपना ऑपरेशन सफल था, यह सफलता = 1 लौटाएगा या सफलता = 0 लौटाएगा। अब मैं सफलता में त्रुटि फेंकना चाहता हूं 0.is है कि संभव है? –
मुझे अपने ऐप पर आज़माएं, तकनीकी रूप से यह संभव होना चाहिए। –