通过POST将参数从IONIC传递给PHP [CODEIGNITER]

时间:2017-09-08 22:06:11

标签: angular codeigniter http ionic-framework ionic3

大家好,我已经创建了一个应用程序,在PHP中使用带有CODEIGNITER的API但是我无法读取我的身体发送的内容,因为在其他情况下看到的比通过POST发送数据 就是这样:

当我从带有HTML的JavaScript发送它时

user = "Ivan More Flowers"

但是当我通过IONIC发送它时就会出现这种情况

user: "Ivan More Flowers"

这已经给我null

  

我在IONIC 3中的代码:

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

@Injectable()
export class UsuarioProvider {

  api: string = 'http://localhost/xxxxxx/Welcome/'

  constructor(public http: Http) {
    console.log('Hello UsuarioProvider Provider');
  }

  verificarUsuario(usuario: string, password: string) {

    let body = { usuario: usuario};

    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });


    return this.http.post(this.api, JSON.stringify(body), {
      headers: headers,
      method: "POST"
    }).map(
      (res: Response) => { return res.json(); }
      );
  }

}
  

并且BACKEND,我的代码是:

function index() {
    $usuario = $this->input->post('usuario');
    $password = $this->input->post('password');        

    echo json_encode($usuario);
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要按照以下所示进行操作。问题的数量取决于您的方法。

  api: string = 'http://localhost/xxxxxx/Welcome/'

  verificarUsuario(usuario: string, password: string) {
    let headers = new Headers();
    headers.append('content-type', 'application/json');
    let body = { usuario: usuario};
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.api, body,options)
            .map((res: Response) => { return res.json();}).
  }