Angular2,Ionic2发布到网络服务 [英] Angular2, Ionic2 post to web service

查看:104
本文介绍了Angular2,Ionic2发布到网络服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;       
    }
}

我正在尝试使用Ionic2创建移动应用程序Angular2 beta。这个应用程序将使用POST向Rails Web服务发送电子邮件。 Rails Web服务工作得很好。我似乎没有开始使用移动应用程序。

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.

推荐答案

存在误解

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

将使用不同的方法

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();
        });              

}

这篇关于Angular2,Ionic2发布到网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆