【问题标题】:Ionic 3 post data to Codeigniter Rest APIIonic 3 将数据发布到 Codeigniter Rest API
【发布时间】:2018-07-24 17:00:34
【问题描述】:

我正在尝试从我的 ionic 应用程序中发布注册表单数据。我使用 ionic 3 和 Codeigniter Rest Api 作为后端。

Codeigniter 休息 API: Codeigniter Rest Server

这是我的ionic post函数

postAuthData(data:any){
  let headers = new Headers();
  headers.append('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
  return new Promise((resolve,reject)=>{
    this.http.post("http://localhost/giftinrest/index.php/api/example/users", data,{headers:headers})
    .subscribe(res => {
      resolve(res.json());
    }, (error) => {
      reject(error);
    });
  })
}

这是我的codeigniter post功能

public function users_post()
{

    $message = [
        'id' => 100, // Automatically generated by the model
        'name' => $this->post('name'),
        'email' => $this->post('email'),
        'message' => 'Added a resource'
    ];

    $this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}

我得到 $this->post('email') 的空值。

我尝试更改标题,但没有成功。

我尝试使用邮递员向http://localhost/giftinrest/index.php/api/example/users 发送请求,并且它有效。

有什么解决方案/建议吗?

谢谢。

【问题讨论】:

    标签: angular codeigniter http-post ionic3


    【解决方案1】:

    我找不到合适的解决方案,但它的工作原理是这样的。我将以下代码放在 codeigniter post 方法中以获取帖子值。

    $post_data = file_get_contents("php://input");
    $request = json_decode($post_data);
    $name = $request->name;
    

    而不是 $this->post()。

    成功了。谢谢

    【讨论】:

      【解决方案2】:

      在 CI 中获取发布数据,您可以这样做:

      $this->input->post();
      

      【讨论】:

      • 不能像下面这样工作 $post_data = file_get_contents("php://input"); $request = json_decode($post_data);
      猜你喜欢
      • 2018-12-23
      • 2018-02-25
      • 2019-08-14
      • 2017-08-09
      • 2017-08-08
      • 2016-08-19
      • 2020-05-04
      • 2020-12-07
      • 2013-06-21
      相关资源
      最近更新 更多