Angular 2 http.post()未发送请求
当我发出post请求时,angular 2 http不会发送此请求Angular 2 http.post()未发送请求,angular,angular2-http,Angular,Angular2 Http,当我发出post请求时,angular 2 http不会发送此请求 this.http.post(this.adminUsersControllerRoute, JSON.stringify(user), this.getRequestOptions()) http post不会发送到服务器,但是如果我这样请求 this.http.post(this.adminUsersControllerRoute, JSON.stringify(user), this.getRequestOptions(
this.http.post(this.adminUsersControllerRoute, JSON.stringify(user), this.getRequestOptions())
http post不会发送到服务器,但是如果我这样请求
this.http.post(this.adminUsersControllerRoute, JSON.stringify(user), this.getRequestOptions()).subscribe(r=>{});
这是有意的吗?如果是,有人能解释一下原因吗?或者它是一个bug?因为
Http
类的post
方法返回一个可观察到的值,您需要订阅它来执行初始化处理。可观测的是懒惰的
有关更多详细信息,请查看此视频:
import { Component, OnInit } from '@angular/core'
import { Http, RequestOptions, Headers } from '@angular/http'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'
import { Post } from './model/post'
import { Observable } from "rxjs/Observable";
@Component({
templateUrl: './test.html',
selector: 'test'
})
export class NgFor implements OnInit {
posts: Observable<Post[]>
model: Post = new Post()
/**
*
*/
constructor(private http: Http) {
}
ngOnInit(){
this.list()
}
private list(){
this.posts = this.http.get("http://localhost:3000/posts").map((val, i) => <Post[]>val.json())
}
public addNewRecord(){
let bodyString = JSON.stringify(this.model); // Stringify payload
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
this.http.post("http://localhost:3000/posts", this.model, options) // ...using post request
.map(res => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')) //...errors if
.subscribe();
}
}
从'@angular/core'导入{Component,OnInit}
从'@angular/Http'导入{Http,RequestOptions,Headers}
导入“rxjs/add/operator/map”
导入“rxjs/add/operator/catch”
从“./model/Post”导入{Post}
从“rxjs/Observable”导入{Observable};
@组成部分({
templateUrl:“./test.html”,
选择器:“测试”
})
用于OnInit的导出类{
职位:可观察
型号:Post=新Post()
/**
*
*/
构造函数(专用http:http){
}
恩戈尼尼特(){
这个列表()
}
私人名单(){
this.posts=this.http.get(“http://localhost:3000/posts)映射((val,i)=>val.json()
}
public addNewRecord(){
让bodyString=JSON.stringify(this.model);//stringify有效负载
let headers=new headers({'Content-Type':'application/json'});//…将Content-Type设置为json
let options=newrequestoptions({headers:headers});//创建请求选项
这是http.post(“http://localhost:3000/posts“,this.model,options)/…使用post请求
.map(res=>res.json())/…并对响应调用.json()以返回数据
.catch((error:any)=>Observable.throw(error.json().error | | |'Server error'))/…如果
.subscribe();
}
}
如果要执行调用,必须订阅返回的observable
另请参见以下角度文档“”
永远订阅!
在您对该方法返回的可观察对象调用subscribe()
之前,该方法不会开始其HTTP请求。所有方法都是如此
系统会自动为您订阅(和取消订阅)
从方法返回的所有观察值都是冷的。HTTP请求的执行被延迟,允许您在实际发生任何事情之前,通过附加操作(如tap
和catchError
)来扩展可观察性
调用subscribe(…)
会触发可观察对象的执行,并导致编写HTTP请求并将其发送到服务器
您可以将这些可观察对象视为实际HTTP请求的蓝图
const req = http.get<Heroes>('/api/heroes');
// 0 requests made - .subscribe() not called.
req.subscribe();
// 1 request made.
req.subscribe();
// 2 requests made.
事实上,每个subscribe()
都会启动一个单独的、独立的可观察执行。订阅两次会导致两个HTTP请求
const req = http.get<Heroes>('/api/heroes');
// 0 requests made - .subscribe() not called.
req.subscribe();
// 1 request made.
req.subscribe();
// 2 requests made.
const req=http.get('/api/heroes');
//已发出0个请求-未调用.subscribe()。
req.subscribe();
//1提出请求。
req.subscribe();
//2提出的请求。
@Thiery无法观看视频,因为它仅限会员观看