angular2 http.post原始地址和目标地址在一起

时间:2016-12-29 14:59:41

标签: http angular post

Angular2 http.post(http://127.0.0.1:555)

Chrome开发者网络秀 找不到post "http://127.0.0.1:4444/127.0.0.1:5555"

"http://127.0.0.1:5555"是节点服务器

"http://127.0.0.1:4444"是angular2服务器

为什么两者叠在一起?

发布组件     从'@ angular / core'导入{Component,OnInit};     从'@ angular / router'导入{ActivatedRoute,Router};     从'./post-detail.service';

导入{PostDetailService}
@Component({
  selector: 'app-post-detail',
  templateUrl: './post-detail.component.html',
  styleUrls: ['./post-detail.component.scss']
})                                                                    
export class PostDetailComponent implements OnInit {
  postDetail= {
    title: '',
    userName: '',
    postWriteTime: 0,
    readTimes: 0,
    commentTimes: 0
  };
  opts = {
  url: 'http:127.0.0.1:5555/postDetail',
  body: '',
};                                                            
  constructor(private router: Router, private activateRoute: ActivatedRoute, private postDetailService: PostDetailService) { }

  ngOnInit() {
    this.activateRoute.params.subscribe((params) => {
      this.opts.body = params['postId'];
      console.log(params, 'params');
    });
   this.getPostDetailById();
  }
  getPostDetailById() {
    this.postDetailService.getPostDetail(this.opts).subscribe(
        res => {
          console.log(res, 'res');
        },
        err => console.log(err),
        () => console.log("complete"));
  }

}
import { Injectable } from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';
@Injectable()
export class PostDetailService {
    constructor(private http: Http) {}
    getPostDetail(opts): Observable<any> {
        console.log(opts, 'opts');
        return this.http.post(opts.url, opts.body).
          map((res: Response) => res.json())
            .catch((error: any) => Observable.throw(error || 'Server error'));
    }
}

1 个答案:

答案 0 :(得分:1)

网址应为

url: 'http://127.0.0.1:5555/postDetail',

而不是

url: 'http:127.0.0.1:5555/postDetail',