我正在设计一种能够起作用并激活它的东西; upSwipe()。当我打电话时,我想发布到一个空身体的网址:http://localhost:8000/areas/fun/736993854/spread/1/。控制台在此帖子中输出正确的URL。但是下面的代码行相同,http.post不工作/不发布。我不太清楚为什么。 这里有什么帮助吗?
import { Component, OnInit } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { FormsModule } from '@angular/forms';
import { Post} from '../_models/index';
import { AuthenticationService, PostService, SpreadService, AreaService} from '../_services/index';
@Component({
templateUrl: 'home.component.html'
})
export class HomeComponent implements OnInit {
posts: Post[] = [];
constructor(private http: Http,
private postService: PostService,
private spreadService: SpreadService,
private areaService: AreaService,
private authenticationService: AuthenticationService) { }
ngOnInit() {
// get posts from secure api end point
this.postService.getPosts()
.subscribe(post => {
this.posts = post;
});
}
upSwipe() {
console.log('upSwipe worked');
const headers = new Headers();
headers.append('Authorization', 'token ' + this.authenticationService.token);
headers.append('Content-Type', 'application/json');
const options = new RequestOptions({
headers: headers,
});
// get areas from api
console.log('http://localhost:8000/areas/' + this.areaService.currentAreaName + '/' + this.posts[0].id + '/spread/1/');
this.http.post('http://localhost:8000/areas/' + this.areaService.currentAreaName + '/' + this.posts[0].id + '/spread/1/', {}, options);
this.ngOnInit();
}
downSwipe() {
console.log('downSwipe worked');
}
fun() {
this.areaService.currentArea = 0;
this.areaService.currentAreaName = 'fun';
this.ngOnInit();
}
information() {
this.areaService.currentArea = 1;
this.areaService.currentAreaName = 'information';
this.ngOnInit();
}
}
答案 0 :(得分:1)
http.post
返回Observable
,Observables在订阅之前不会开始发送。试试这个:
this.http.post(url, {}, options).subscribe(
data => console.log(data)
);