【问题标题】:Pass parameters by POST from IONIC to PHP [CODEIGNITER]通过 POST 从 IONIC 传递参数到 PHP [CODEIGNITER]
【发布时间】:2018-02-17 21:20:28
【问题描述】:

大家好,我创建了一个应用程序,使用 PHP 中的 API 和 CODEIGNITER,但我无法读取我的正文它发送的内容,因为它在其他情况下被看到,而不是通过 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);
}

【问题讨论】:

    标签: angular codeigniter http ionic-framework ionic3


    【解决方案1】:

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

      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();}).
      }
    

    【讨论】:

    • 您好,非常感谢您的回答,但现在我知道该方法是“OPTIONS”,我不知道该怎么办:c,有些没有为“HTTP”安装
    • 数据以这种方式发送给我会发生什么情况:i.stack.imgur.com/lr4VI.png
    猜你喜欢
    • 2017-08-09
    • 2012-06-23
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多