2016-10-27 74 views
1

我有()一個模板,(點擊)triggets addUseCase:HTTP崗位工作的郵遞員,但沒有角2

import {Component} from "@angular/core"; 
import {DataService} from "../shared/data.service"; 
import {Config} from "../shared/strings"; 
import {Headers, Http} from "@angular/http"; 

@Component({ 
    selector: "add", 
    styleUrls: ["add.component.scss"], 
    templateUrl: "add.component.html" 
}) 
export class AddComponent { 
    public title: string; 
    public body: string; 

    constructor(private dataService: DataService, 
       private http: Http) { 
     this.title = ""; 
     this.body = ""; 
    } 

    public addUseCase() { 

     let url = Config.baseUrl + Config.case; 
     console.log("url", url); // --> http://localhost:3001/case 

     let headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 

     let body = JSON.stringify({title:this.title}) 

    this.http.post(url, body, {headers: headers}).map(res => console.log ("res", res), error => console.log("error", error)); 
    } 
} 

在郵差測試後,我讓我的節點/ Express服務器正確的反應。但是,當我嘗試使用HTTP角送我沒有得到什麼反應,沒有錯誤:

我郵遞員的工作,但不能使角2工作HTTP POST:

enter image description here

更新與錯誤:

this.http.post(url, body, {headers: headers}) 
    .map(res => res.json()).subscribe(data => console.log(data)); //...errors if any 
; 

而且錯誤:

EXCEPTION: Unexpected token W in JSON at position 0ErrorHandler.handleError @ core.umd.js:3462next @ core.umd.js:6924schedulerFn @ core.umd.js:6172SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ core.umd.js:6164onError @ core.umd.js:6388onHandleError @ core.umd.js:6263ZoneDelegate.handleError @ zone.js:236Zone.runTask @ zone.js:157ZoneTask.invoke @ zone.js:335 
core.umd.js:3467 ORIGINAL STACKTRACE:ErrorHandler.handleError @ core.umd.js:3467next @ core.umd.js:6924schedulerFn @ core.umd.js:6172SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ core.umd.js:6164onError @ core.umd.js:6388onHandleError @ core.umd.js:6263ZoneDelegate.handleError @ zone.js:236Zone.runTask @ zone.js:157ZoneTask.invoke @ zone.js:335 
core.umd.js:3468 SyntaxError: Unexpected token W in JSON at position 0 
    at JSON.parse (<anonymous>) 
    at Function.Json.parse (http://localhost:3000/vendor.bundle.js:11272:59) 
    at Response.Body.json (http://localhost:3000/vendor.bundle.js:12250:30) 
    at MapSubscriber.project (http://localhost:3000/main.bundle.js:6202:47) 
    at MapSubscriber._next (http://localhost:3000/vendor.bundle.js:23914:36) 
    at MapSubscriber.Subscriber.next (http://localhost:3000/vendor.bundle.js:249:19) 
    at XMLHttpRequest.onLoad (http://localhost:3000/vendor.bundle.js:12581:43) 
    at ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:14882:36) 
    at Object.onInvokeTask (http://localhost:3000/vendor.bundle.js:6930:42) 
    at ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:14881:41) 
    ------------- Elapsed: 16 ms; At: Thu Oct 27 2016 08:06:32 GMT-0500 (COT) ------------- 
    at Object.onScheduleTask (http://localhost:3000/polyfills.bundle.js:14537:19) 
    at ZoneDelegate.scheduleTask (http://localhost:3000/polyfills.bundle.js:14859:50) 
    at Zone.scheduleEventTask (http://localhost:3000/polyfills.bundle.js:14791:40) 
    at zoneAwareAddListener (http://localhost:3000/polyfills.bundle.js:15441:15) 
    at XMLHttpRequest.addEventListener (eval at createNamedFn (http://localhost:3000/polyfills.bundle.js:15544:18), <anonymous>:3:43) 
    at Observable._subscribe (http://localhost:3000/vendor.bundle.js:12624:23) 
    at Observable.subscribe (http://localhost:3000/vendor.bundle.js:70:28) 
    at Observable._subscribe (http://localhost:3000/vendor.bundle.js:128:29)ErrorHandler.handleError @ core.umd.js:3468next @ core.umd.js:6924schedulerFn @ core.umd.js:6172SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ core.umd.js:6164onError @ core.umd.js:6388onHandleError @ core.umd.js:6263ZoneDelegate.handleError @ zone.js:236Zone.runTask @ zone.js:157ZoneTask.invoke @ zone.js:335 
http.umd.js:188 Uncaught SyntaxError: Unexpected token W in JSON at position 0(…)Json.parse @ http.umd.js:188Body.json @ http.umd.js:1166(anonymous function) @ add.component.ts:34MapSubscriber._next @ map.js:77Subscriber.next @ Subscriber.js:89onLoad @ http.umd.js:1497ZoneDelegate.invokeTask @ zone.js:265onInvokeTask @ core.umd.js:6233ZoneDelegate.invokeTask @ zone.js:264Zone.runTask @ zone.js:154ZoneTask.invoke @ zone.js:335 
+0

是否有任何錯誤,你越來越? – ranakrunal9

+0

您從未訂閱過可觀測的 –

+0

@peeskillet如何訂閱? – commonSenseCode

回答

3

您從未訂閱過Observable。沒有請求將被做出,直到你這樣做

this.http.post(url, body, {headers: headers}) 
    .map(res => res.text()).subscribe(data => console.log(data)); 
相關問題