2
當(A)中列出的代碼在調用test()
時從不發送它的HTTP請求,而(B)中的幾乎相同的代碼確實工作。任何想法是什麼問題?我知道請求是否通過觀看服務器日誌發送。另外,如果我通過手動將(A)中的請求粘貼到瀏覽器中,我會得到預期的響應。我很難過!Angular2不發送HTTP GET
(A)
import { Constants } from '../toplevel/constants'
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class SigninService {
constructor (private http: Http, private constants : Constants) {
}
getToken(username: string, password: string) : Observable<string>{
var url = `${this.constants.apiRoot}/users/${username}?${password}`
console.log(`Calling api server with url ${url}`)
return this.http.get(url)
.map((res:Response) => JSON.stringify(res.json()))
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
test() {
this.getToken('jc', 'yadayada')
}
}
(B:摘錄)
另外,我看到張貼的訊息'console.log' –
從(A)的代碼,你有一個錯誤代碼或什麼?你可以嘗試在catch的旁邊添加'.subscribe()'來查看嗎? – soywod
你使用調試器嗎?在你的功能,看看發生了什麼一步一步? –