![](/img/trans.png)
[英]How to post data from ionic to restfull api (Codeigniter)?
[英]Ionic 3 post data to Codeigniter Rest API
我正在嘗試從我的離子應用程序發布注冊表單數據。 我使用ionic 3和Codeigniter Rest Api作為后端。
Codeigniter Rest Api: Codeigniter Rest服務器
這是我的離子后功能
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發布功能
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')的null。
我嘗試更改標題,但是沒有用。
我嘗試了對http://localhost/giftinrest/index.php/api/example/users的郵遞員發布請求,並且它可以正常工作。
有什么解決方案/建議嗎?
謝謝。
我找不到合適的解決方案,但它的工作原理是這樣的。 我將以下代碼放在codeigniter post方法內以獲取post值。
$post_data = file_get_contents("php://input");
$request = json_decode($post_data);
$name = $request->name;
而不是$ this-> post()。
有效。 謝謝
在CI中獲取帖子數據,您可以執行以下操作:
$this->input->post();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.