【问题标题】:POST data in server - angular 4在服务器中发布数据 - 角度 4
【发布时间】:2017-10-11 08:21:16
【问题描述】:

我创建了一个小程序,用于发布数据并将结果返回到服务器中。我在 .html 中创建了一个按钮,并且功能正常。一旦单击,我就会从服务器端获取访问日志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>

【问题讨论】:

  • 您的服务器在处理完 post 请求后还会返回数据吗?
  • 它应该返回一个数据..,到目前为止,当我发布数据时,我只能在服务器端获得 access.log
  • 您必须将后期数据分配给控制器中的变量。 this.someVar = result.
  • 够了,我应该在哪里提供要发布的数据

标签: mysql angular angular4-httpclient


【解决方案1】:

因此,更好的方法是在 AppComponent 上创建一个属性,例如“结果”。

 export class AppComponent {
   result;
 ...

然后在订阅下一节中类似:

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

然后在 app.component.html 中:

<div>{{result}}</div>

【讨论】:

  • 请耐心等待..,还有一个问题..,我应该在哪里声明我应该发布到服务器的数据
  • 如果你提到“内容”,那取决于它来自哪里。也许来自一个表单(然后是 FormGroup),但通常也是类中的一个变量
  • 我已经对上面的 .ts 文件进行了更改。我做对了吗
  • 我看不清楚那个 xml 片段在那里做了什么,我只会做:this.result = data;并删除那个 .. 毕竟它应该来自服务器不应该吗?
  • 我已经删除了 xml 部分并按照你说的做了......它访问了服务器日志。但是服务器没有结果......
猜你喜欢
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多