POST 服务器中的数据 - angular 4
POST data in server - angular 4
我创建了一个小程序,用于 post 数据并将结果返回 server.I 在 .html 中创建了一个按钮,功能正常 fine.I 单击 POST 按钮后,我从服务器端获取访问日志。但是我无法显示数据。我应该再次使用 GET 函数还是有什么简单的方法?
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>
所以更好的方法是在 AppComponent 上创建一个属性,例如 'result'。
export class AppComponent {
result;
...
然后在订阅的下一节中,如下所示:
.subscribe((data) => {
this.result = data;
})
然后在 app.component.html:
<div>{{result}}</div>
我创建了一个小程序,用于 post 数据并将结果返回 server.I 在 .html 中创建了一个按钮,功能正常 fine.I 单击 POST 按钮后,我从服务器端获取访问日志。但是我无法显示数据。我应该再次使用 GET 函数还是有什么简单的方法?
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>
所以更好的方法是在 AppComponent 上创建一个属性,例如 'result'。
export class AppComponent {
result;
...
然后在订阅的下一节中,如下所示:
.subscribe((data) => {
this.result = data;
})
然后在 app.component.html:
<div>{{result}}</div>