Jon
Jon

Reputation: 67

Angular2, Ionic2 post to web service

import {Page, Platform} from 'ionic-angular';
import { Http, Headers, Response, RequestOptions, HTTP_PROVIDERS} from 'angular2/http';
import { bootstrap } from 'angular2/platform/browser';
import { Component, Injectable, Inject } from 'angular2/core';
import 'rxjs/Rx';
import { CORE_DIRECTIVES, FORM_DIRECTIVES } from 'angular2/common';
import {Observable}     from 'rxjs/Observable';

@Page({
directives: [ CORE_DIRECTIVES, FORM_DIRECTIVES ],  
templateUrl: 'build/pages/getting-started/getting-started.html'
})

@Injectable()
export class GettingStartedPage {
    public platform;
    public networkState;
    private headers;
    private _apiUrl = 'http://172.16.2.115:3004/message'; 
    subject: string;
    message: string;
    comments: string;

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

    onSubmit(value) {
        this.send(value.subject, value.body);
    }

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

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

        this.send(subject, body).subscribe(res => {
            console.log(message);
            console.log(this._apiUrl);
        });

        return result;       
    }
}

I am trying to create a mobile app using Ionic2 and Angular2 beta.This app will send an email using POST to a Rails web service. The Rails web service is working just fine. I dont seem to be getting to work the mobile app.

Upvotes: 1

Views: 483

Answers (2)

Thierry Templier
Thierry Templier

Reputation: 202146

You should refactor your code this way:

onSubmit(value) {
    this.send(value.subject, value.body).subscribe(res => {
        console.log(message);
        console.log(this._apiUrl);
    });

}

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

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

    return result;       
}

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657158

There was a misunderstanding

   this.send(subject, body).subscribe(res => {
        console.log(message);
        console.log(this._apiUrl);
    });

would be in a different method

onSubmit(value) {
    this.send(value.subject, value.body)
    .subscribe(res => {
        console.log(message);
        console.log(this._apiUrl);
    });
}

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

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

}

Upvotes: 1

Related Questions