在angular2中使用订阅调用成功,错误回调?
calling success, error callbacks using subscribe in angular2?
给予 responce.json () 不适用于我的情况
component.ts
this.AuthService.loginAuth(this.data).subscribe(function(response) {
console.log("Success Response" + response)
},
function(error) {
console.log("Error happened" + error)
},
function() {
console.log("the subscription is completed")
});
AuthService.ts
loginAuth(data): Observable<any> {
return this.request('POST', 'http://192.168.2.122/bapi/public/api/auth/login', data,{ headers:this. headers })
.map(response => response)
//...errors if any
.catch(this.handleError);
}
给予 [object,objet]
如果我把像 .map(response => response.json()) 这样的地图函数服务给出像 responce.json () is not function
这样的错误
请帮帮我
尝试使用这种结构:
this.AuthService.loginAuth(this.data).subscribe(
suc => {
console.log(suc);
},
err => {
console.log(err );
}
);
您可能还想对发送到服务器的数据进行字符串化,例如:
loginAuth(data) {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
var info = JSON.stringify(data);
return this._http.request("http://192.168.2.122/bapi/public/api/auth/login", info , { headers: headers }).map(res => res.json())
}
并且您必须在引用 Http 的服务的构造函数中声明一个变量,如下所示:
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
constructor(private _http: Http) {
}
这就是我的工作方式
在您的服务页面中
//Import if needed
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
let options = new RequestOptions({ headers: this.headers });
return this.http.post('http://192.168.2.122/bapi/public/api/auth/login', data, options)
.map((res: Response) => {
return { "res": res.json() };
})
.catch((e: any) => {
console.log(e.status);
return Observable.throw({"Errors":e.json()});
});
还有你的模板ts文件
this.AuthService.loginAuth(this.data).subscribe((result: any) => {
let res = result.res;
console.log(res)
},(error: any) => {
if(typeof error['Errors'] !="undefined"){
console.log(error['Errors']);
}
});
它非常适合我
给予 responce.json () 不适用于我的情况
component.ts
this.AuthService.loginAuth(this.data).subscribe(function(response) {
console.log("Success Response" + response)
},
function(error) {
console.log("Error happened" + error)
},
function() {
console.log("the subscription is completed")
});
AuthService.ts
loginAuth(data): Observable<any> {
return this.request('POST', 'http://192.168.2.122/bapi/public/api/auth/login', data,{ headers:this. headers })
.map(response => response)
//...errors if any
.catch(this.handleError);
}
给予 [object,objet] 如果我把像 .map(response => response.json()) 这样的地图函数服务给出像 responce.json () is not function
这样的错误请帮帮我
尝试使用这种结构:
this.AuthService.loginAuth(this.data).subscribe(
suc => {
console.log(suc);
},
err => {
console.log(err );
}
);
您可能还想对发送到服务器的数据进行字符串化,例如:
loginAuth(data) {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
var info = JSON.stringify(data);
return this._http.request("http://192.168.2.122/bapi/public/api/auth/login", info , { headers: headers }).map(res => res.json())
}
并且您必须在引用 Http 的服务的构造函数中声明一个变量,如下所示:
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
constructor(private _http: Http) {
}
这就是我的工作方式
在您的服务页面中
//Import if needed
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
let options = new RequestOptions({ headers: this.headers });
return this.http.post('http://192.168.2.122/bapi/public/api/auth/login', data, options)
.map((res: Response) => {
return { "res": res.json() };
})
.catch((e: any) => {
console.log(e.status);
return Observable.throw({"Errors":e.json()});
});
还有你的模板ts文件
this.AuthService.loginAuth(this.data).subscribe((result: any) => {
let res = result.res;
console.log(res)
},(error: any) => {
if(typeof error['Errors'] !="undefined"){
console.log(error['Errors']);
}
});
它非常适合我