我想将带有http.post方法的Ionic 2应用程序的数据发布到服务器,但服务器返回undefined。
我曾尝试过发送一次字符串,但结果是一样的。
这是Angular 2客户端代码:
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { RegisterPage } from '../register/register';
import { Http, Headers } from '@angular/http';
import { ToastController } from 'ionic-angular';
import { User } from '../../providers/user';
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
private accounts: Object[] = []
private headers: Headers;
private http;
private toast;
public email;
public password;
public user: User;
constructor(public navCtrl: NavController, public navParams: NavParams,
http: Http, public toastCtrl: ToastController) {
this.http = http;
this.toast = toastCtrl;
}
ngAfterViewInit() {
}
toastMessenger(res : string) {
let toast = this.toast.create({
message: res,
duration: 3000
});
toast.present();
}
submit() {
let headers = new Headers();
headers.append("Content-Type", 'application/json');
console.log(this.email)
this.http.post('http://localhost:3000/validation', JSON.stringify(this.email), { headers: headers })
.subscribe(() => {
console.log("Dados enviados com sucesso");
});
}
changeToRegister() {
this.navCtrl.push(RegisterPage);
}
}
这是我的动词后路线:
app.post('/validation', function(req, res) {
console.log(req.body); // => undefined
});