(A)中列出的代码在调用test()
时从不发送其HTTP请求,而(B)中的几乎相同的代码确实有效。知道问题是什么吗?我知道是否通过观察服务器日志发送请求。此外,如果我通过将(A)中的请求粘贴到浏览器中手动进行请求,我会得到预期的响应。我很难过!
(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:摘录)
getDocument(id: string) : Observable<Document>{
return this.http.get(`${this.apiUrl}/documents/${id}`)
.map((res:Response) => new Document(res.json()['document']))
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
答案 0 :(得分:2)
你的序列很冷。您需要将测试更改为:
this.getToken('jc', 'yadayada').subscribe()
使其处于活动状态并发送请求