POST形式Angular2

时间:2017-05-12 09:59:01

标签: node.js rest angular authentication post

我尝试使用带有NodeJS API的Angular2作为后端服务器来执行登录表单。

我无法向API发布任何请求。 这是我的前代码示例:

private authenticate_url = "Server_URL:PORT/authenticate";
login(username: string, password: string): Observable<User> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    console.log("Username : "+username+" Password : "+password);
    return this.http.post(this.authenticate_url, { username, password }, options)
                .map(this.extractData)
                .catch(this.handleError)
}

当我检查网络活动时,没有执行POST请求。

1 个答案:

答案 0 :(得分:0)

你需要使用RxJS的.subscribe()方法来实际执行查询,所以你需要像这样编写它:

return this.http.post(this.authenticate_url, { username, password }, options)
                .map(this.extractData)
                .catch(this.handleError)
                .subscribe((queryResult) => {console.log(queryResult)})

或者您可以在以下功能之外完成:

login("baksdasd", "asfasfasdf").subscribe((queryResult) => {console.log(queryResult)})

您还需要导入您使用的运算符:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

在这里,您有一个详细的教程来解决您可能遇到的任何其他问题:https://scotch.io/tutorials/angular-2-http-requests-with-observables

相关问题