2017-10-11 57 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文件上面的变化的变量,我是否正确做 –