简体   繁体   English

Angular2发布到Web服务

[英]Angular2 post to web service

constructor(platform: Platform, public http: Http) {
    this.platform = platform;
    this.headers = new Headers();
    this.headers.append('Content-Type', 'application/x-www-form-urlencoded');
}



send(subject, body)
{
    var body = "subject=" + subject + "&body=" + body;

    let result = this.http.post('http://172.16.2.115:3004/message',
        body, 
        {
            headers: this.headers
        });


    console.log(body);
    console.log(this._apiUrl);

    return result;         
}

I am trying to post a message to a Ruby on Rails web service using Ionic2 and Angular2 beta. 我正在尝试使用Ionic2和Angular2 beta将消息发布到Ruby on Rails Web服务。 The web service works just fine, the issue is that the ionic app doest seem to be posting the message. Web服务工作正常,问题是Ionic应用似乎没有发布消息。 Does this look right? 这看起来正确吗?

You need to subscribe() otherwise no request will be sent 您需要subscribe()否则将不会发送任何请求

send(subject, body)
{
    var body = "subject=" + subject + "&body=" + body;

    let result = this.http.post('http://172.16.2.115:3004/message',
        body, 
        {
            headers: this.headers
        })
    .subscribe(res => {
      this.comments = res.json();
      console.log(body);
      console.log(this._apiUrl);

      // no effect here
      // return result;             
    });  
}

You need to move the code that processes the repsonse into subscribe() otherwise it will be executed before the response arrived. 您需要将处理回复的代码移到subscribe()否则它将在响应到达之前执行。 You can't return the result, you can only return the observable for someone else to subscribe. 您无法返回结果,只能返回可观察的结果,以供其他人订阅。

send(subject, body)
{
    var body = "subject=" + subject + "&body=" + body;

    return this.http.post('http://172.16.2.115:3004/message',
        body, 
        {
            headers: this.headers
        });
    .map(res => {
      this.comments = res.json();
    });  
}
this.send.subscribe(res => {
   console.log(body);
   console.log(this._apiUrl);
});

You need to subscribe to the observable to make it execute since observables are lazy. 由于可观察对象是惰性的,因此您需要订阅可观察对象以使其执行。

If you want to return the result to the caller, you can subscribe within the calling method. 如果要将结果返回给调用方,则可以在调用方法中进行预订。 Don't forget that the request is executed asynchronously, so you will receive the response data in the callback specified in the subscribe method. 不要忘记该请求是异步执行的,因此您将在subscription方法中指定的回调中接收响应数据。

this.service.send(asunto, cuerpo).subscribe((result) => { // <------
  // do something with the result
});

In this case, the send method can remain the same: 在这种情况下, send方法可以保持不变:

send(asunto, cuerpo) {
  var body = "subject=" + asunto + "&body=" + cuerpo;

  return this.http.post(this._apiUrl,
    body, {
        headers: this.headers
    }).map(res => res.json());
}

If you're interested in how to organize your code to interact with an HTTP service, you could have a look at this question: 如果您对如何组织代码以与HTTP服务进行交互感兴趣,可以查看以下问题:

Moreover you could leverage the URLSearchParams class to buld your form content: 此外,您可以利用URLSearchParams类来获取表单内容:

let content = new URLSearchParams();
content.set('subject', asunto);
content.set('body', cuerpo);

var inputPayload = content.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM