0

I want to post form data and retrive the same from post method but it fails. It posts nothing to the external link. I have created external link in PHP. What is the problem?

This is the complete code. I'm using HTTP from from '@angular/http'. This is the complete code

my home.html code is bellow home.html

<ion-header>
 <ion-navbar>
 <ion-title>
 Ionic3 Server Send Test
 </ion-title>
 </ion-navbar>
</ion-header>

<ion-content padding>
 <ion-list>
 <ion-item>
 <ion-label floating>id</ion-label>
 <ion-input type="text" name="id" [(ngModel)]="data.id"></ion-input>
 </ion-item>

 <ion-item>
 <ion-label floating>Username</ion-label>
 <ion-input type="text" name="name" [(ngModel)]="data.name"></ion-input>
 </ion-item>

 <button ion-button block (click)="submit()">Submit to server</button>
 </ion-list>

 <ion-card>
 <ion-card-header>
 Response
 </ion-card-header>

 <ion-card-content>
 <b>{{data.response.id}}</b>
 <b>{{data.response.name}}</b>
 </ion-card-content>
 </ion-card>
</ion-content>

my home.ts code is bellow

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http'; //https://stackoverflow.com/questions/43609853/angular-4-and-ionic-3-no-provider-for-http

@Component({
 selector: 'page-home',
 templateUrl: 'home.html'
})

export class HomePage {
 data:any = {};

 constructor(public navCtrl: NavController, public http: Http) {
 this.data.name = '';
 this.data.id = '';
 this.data.response = '';

 this.http = http;
 }

 submit() {
 var link = 'http://127.0.0.1:3000/passdata';
 var myData = JSON.stringify({id: this.data.id, name: this.data.name});

 this.http.post(link, myData)
 .subscribe(data => {
 this.data.response = data["_body"]; //https://stackoverflow.com/questions/39574305/property-body-does-not-exist-on-type-response
 }, error => {
 console.log("Oooops!");
 });
 }
}
user107224
  • 205
  • 2
  • 12
Akshay katale
  • 137
  • 3
  • 10

1 Answers1

0

Can you please try it on chrome browser with developer tool open, and show us the network tab, see if there's any request fail and paste the error in the post.

From your code, I can see you forgot to specify "Content-Type": "application/json" in the request header.

And you probably have to remove this.http = http; from your constructor.

Charles Zhao
  • 158
  • 11