我正在開發一個angular2作爲前端的應用程序我想從客戶端登錄到服務器端,但是當我嘗試傳遞憑據時,我在瀏覽器控制檯中出現了這些錯誤。Angular2:data.json不是函數
在運行時,它抱怨有:
data.json is not a function
這是我的服務代碼:
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class LoginService {
isAuthenticated: boolean = true;
constructor(private http: Http) {
}
login(username: string, password: string) {
const headers = new Headers();
const creds = 'username=' + username + '&password=' + password;
headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return new Promise((resolve) => {
this.http.post('http://localhost:8080/StudentManager/login', creds, { headers: headers })
.map(this.extractData)
.subscribe(
(data) => {
if(data.json().success) {
window.localStorage.setItem('auth_key', data.json().token);
console.log(username);
this.isAuthenticated = true;
}
resolve(this.isAuthenticated);
});
}
);
}
}
以下是錯誤的快照:
它的工作,錯誤消失了,但我不知道爲什麼你的映射數據JSON它不會進入'if(data.success)'裏面,即使我把正確的憑證 – SuperGirl
檢查'data.success'的值 –