POST with URLSearchParams (); It does not work

0

Hello I try to make a post for a login and I do not send the data, this is the code .. IONIC

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

import { URL_SERVICES } from '../../config/url.services';

import { AlertController } from 'ionic-angular';

@Injectable()
export class UsersProvider {

  token:string;
  id_user:string;

  constructor(public http: Http,
              private alertCtrl: AlertController) {
    console.log('Hello UsersProvider Provider');
  }

  ingresar( email:string, password:string){
    console.log("usersP ",email);
    console.log("usersP ", password);

    let data = new URLSearchParams();
    data.append("email", email);
    data.append("password", password);

    let url = URL_SERVICES + "login";

    return this.http.post( url, data )
                    .map( resp => {

                      let data_resp = resp.json();
                      console.log( data_resp );

                      if( data_resp.err ){
                        this.alertCtrl.create({
                          title: 'Error al iniciar',
                          subTitle: data_resp.message,
                          buttons: ['OK']
                        }).present();

                      }else{
                        this.token = data_resp.token;
                        this.id_user = data_resp.id_user;

                        //GUARDAR STORAGE
                      }

                    });

  }

}

the url.config file

export const URL_SERVICES = "http://localhost/appWC/index.php/";

the login.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

import { URL_SERVICES } from '../../config/url.services';

import { AlertController } from 'ionic-angular';

@Injectable()
export class UsersProvider {

  token:string;
  id_user:string;

  constructor(public http: Http,
              private alertCtrl: AlertController) {
    console.log('Hello UsersProvider Provider');
  }

  ingresar( email:string, password:string){
    console.log("usersP ",email);
    console.log("usersP ", password);

    let data = new URLSearchParams();
    data.append("email", email);
    data.append("password", password);

    let url = URL_SERVICES + "login";

    return this.http.post( url, data )
                    .map( resp => {

                      let data_resp = resp.json();
                      console.log( data_resp );

                      if( data_resp.err ){
                        this.alertCtrl.create({
                          title: 'Error al iniciar',
                          subTitle: data_resp.message,
                          buttons: ['OK']
                        }).present();

                      }else{
                        this.token = data_resp.token;
                        this.id_user = data_resp.id_user;

                        //GUARDAR STORAGE
                      }

                    });

  }

}

the login.html

<ion-item>
  <ion-label fixed>Correo </ion-label>
  <ion-input type="email" [(ngModel)]="email"></ion-input>
</ion-item>

<ion-item>
  <ion-label fixed>Contraseña </ion-label>
  <ion-input type="password" [(ngModel)]="password"></ion-input>
</ion-item>
    
asked by Edwin Anaya 18.10.2017 в 20:36
source

1 answer

1

You are using a module in Angular without importing it! , you must import the module

 import {URLSearchParams} from '@angular/http';

more about the module you can see the documentation

Additional

Since you have already loaded a @angular/http module, you can do

import { Http , URLSearchParams } from '@angular/http';

So your code is aesthetically better

    
answered by 18.10.2017 / 21:16
source