2017-10-11 105 views
0

我已經創建了一個小程序,用於發佈數據並將結果返回到服務器中。我在.html中創建了一個按鈕,並且函數可以正常工作。我正在獲取訪問日誌從服務器端一旦點擊POST按鈕。但我無法顯示數據。我應該再次使用GET函數還是有任何簡單的方法?在服務器的POST數據 - 角4

app.component.ts 

import {Injectable, Component } from '@angular/core'; 
import { Http, Response, RequestOptions, Headers} from '@angular/http'; 
@Component({ 
selector: 'app-root', 
templateUrl:'app.component.html' 
}) 

export class AppComponent { 
result; 
constructor (private http:Http) { 

} 

executeHttp() { 
var headers = new Headers(); 
//this.createAuthorizationHeader(headers); 
headers.append('Content-Type', 'application/json'); 
var content = JSON.stringify({ 
name: 'my name' 
}); 

    return this.http.post(
'http://100.000.00.000/webservices/voltage-info-service/server/server.php', content, { 
    headers: headers 
    }).map(res => res.json()).subscribe((data) 

    => { '<?xml version="1.0" encoding="utf-8"?><lfc:requests><lfc:request><lfc:busID>66</lfc:busID><lfc:timestamp>223456789</lfc:timestamp><lfc:coordinates>'+ 
    '<lfc:LongD>8</lfc:LongD><lfc:LongM>6</lfc:LongM><lfc:LongS>25.599</lfc:LongS><lfc:LatD>51</lfc:LatD><lfc:LatM>33</lfc:LatM><lfc:LatS>23.9898</lfc:LatS>'+ 
    '</lfc:coordinates></lfc:request></lfc:requests>';}, 

    this.result =data; 
    console.log("data"); }, 
    err => { console.log(err); } 
); 
    } 
    } 

    app.component.html 

    <li> 
    <button type="submit" (click)="executeHttp()">SOAP request </button> 
    </li> 
+0

是您的服務器甚至返回數據? –

+0

它應該返回一個數據..截至目前我只在服務器端獲取access.log當我發佈數據 –

+0

您必須將後數據分配給控制器中的變量。 'this.someVar = result'。 – John

回答

1

所以一個更好的方法是如果你在AppComponent上創建一個屬性,比如'result'。

export class AppComponent { 
    result; 
... 

然後在認購下一節是這樣的:

.subscribe((data) => { 
    this.result = data; 
}) 

,然後在app.component.html:處理POST請求後

<div>{{result}}</div> 
+0

熊與我..,一個更多的問題..,我應該在哪裏申報我的數據,我應該發佈到服務器 –

+0

如果你參考'內容「,這取決於它來自何處。也許從一個表單(FormGroup然後),但通常也在類 – Zolcsi

+0

我已經對.ts文件上面的變化的變量,我是否正確做 –