2016-07-29 19 views
0

越來越不確定我嘗試後「UID」但服務器顯示「未定義」,我不知道爲什麼會這樣!即使我正確設置標題,它仍然無效,請幫助我。角2個TS - 在POST在服務器上

這裏應用程序的代碼:

HTML

<form (ngSubmit)="onSubmit()"> 
    User Id: <input type="text" name="uid" [(ngModel)] = 'uid'> <br> 
    <input type="submit" value="Submit"> 
</form> 

的onsubmit功能

onSubmit(value) { 
    var headers = new Headers(); 
    var data = { uid: this.uid }; 
    headers.append('Content-Type', 'application/json'); 
    this.http.post('http://192.168.50.193:1000/process_post', JSON.stringify(data), {headers: headers}) 
     .subscribe((data)=> 
      console.log("POST::: "+data); 
    ); 
} 

server.js(快遞)POST方法在這裏:

app.post('process_post', urlencodedParser, function (req, res) { 
 
    response = { 
 
     first_name:req.body.uid 
 
    }; 
 
    console.log(req.body.uid); 
 
    res.end(JSON.stringify(response)); 
 
})

點擊後提交命令提示符我得到'未定義',爲什麼呢?

+0

是'的'的onsubmit()'方法this.uid'不確定?你可以嘗試用['Postman'](https://www.getpostman.com/)複製請求 - 你也可以在那裏找到'undefined'嗎? – rinukkusu

+0

你可以嘗試''console.log(this.uid)''onSubmit'方法內' –

+0

是的,我使用** onSubmit()**方法**'this.uid'**得到正確的值。 – Shree

回答

0

嘗試map方法:

onSubmit(value) { 
    var headers = new Headers(); 
    var data = { uid: this.uid }; 
    headers.append('Content-Type', 'application/json'); 
    this.http.post('http://192.168.50.193:1000/process_post', JSON.stringify(data), {headers: headers}) 
     .map(data => data.json()) 
     .subscribe((data)=> 
      console.log("POST::: "+data); 
    ); 
} 
+0

謝謝,但控制檯仍然顯示空白對象。 – Shree