提供 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()) 这样的 map 函数服务给出像 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) {
}
这就是我的工作方式
关于angular - 在angular2中使用订阅调用成功,错误回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42202997/