2016-12-29 27 views
0

Angular2 http.post(http://127.0.0.1:555)angular2 http.post原地址和目的地址是一起

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

請發佈允許複製的代碼,並請編輯您的問題並將其添加,因爲評論中的代碼通常無法讀取。 –

+0

代碼低於 – teenth

回答

0

URL應該代替

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

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

非常感謝 – teenth