Ionic - request api php

Asked

Viewed 297 times

0

Guys I’m trying to make a request will post on a php api, it returns me Undefined. My predecessor:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
  Generated class for the ProviderAccessApi provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class ProviderAccessApi {
  host = "http://localhost/bullGreenCollect/";
  data: any;

  constructor(public http: Http) {
    this.data = null;
  }

autentication() {
    /*if (this.data) {
      // already loaded data
      return Promise.resolve(this.data);
    }*/

    // don't have the data yet
    return new Promise(resolve => {
        var url = this.host + 'P.php';
        var params = {name:'Augusto'};
      this.http.post(url, params)
        .map(res => res.json())
        .subscribe(data => {
          // we've got back the raw data, now generate the core schedule data
          // and save the data for later reference
          this.data = data.results;
          console.log(this.data);
          resolve(this.data);
        });
    });
  }


}

my php:

<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }


$postdata = file_get_contents("php://input");
$decoded = json_decode($postdata);

$array = array('sl' =>$decoded->name);
echo json_encode($array);

Does anyone know why he did not return the post parameter I passed ?

1 answer

1


This Provider log console puts date and not this.data, see if it returns something different.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.