Codeigniter:发布在Codeigniter apis中不起作用的数据
i am using following code to test posting of data but post data is always empty i tried to test using postman but it did not work , following is a test code that i wrote but it always goes to else block , i tried writing else if as well but it did not work
class Api_home extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Api_model','api');
$this->load->helper('form');
$data= array(
'message' => ' Something went wrong',
'status' =>1,
'data' =>'',
);
}
public function test(){
$lang= $this->input->post['lang'];
if($lang=="ar"){
$this->data['message']= 'Arabic test';
}
//else if
// else if($lang=="en")
else{
$this->data['message']= 'English test';
}
$data['status']= 1 ;
echo json_encode($data,true);
die;
}
it always goes to en version even if i post ar , if i do var_dump for posted data it gives me false . Please advise how can i sort it
i always get following response
我使用以下代码测试数据发布但发布数据总是空的我试图使用postman测试但是它 没有工作,以下是我写的测试代码,但它总是去其他块,我尝试写其他如果,但它不起作用 p>
类Api_home扩展CI_Controller {
function __construct(){
parent :: __ construct();
$ this-> load-> model('Api_model','api');
$ this-> load- > helper('form');
$ data = array(
'message'=>'出错了',
'状态'=> 1,
'数据'=>'' ,
);
}
公共函数test(){
$ lang = $ this-> input-> post ['lang'];
if($ lang ==“ar”){
$ this-> data ['message'] ='阿拉伯语测试';
}
//其他如果
// else if if($ lang ==“en”)
else {
$ this-&gt ; data ['message'] ='英语测试';
}
$ data ['status'] = 1;
echo json_encode($ data,true);
cn;
}
code> pre>
即使我发布了ar,它总是会转到版本,如果我为发布的数据执行var_dump它会给我错误。 请告知我如何对其进行排序 p>
我总是得到以下回复 p>
div>
Fortunately I'm working on a CI project atm so it wasn't too much effort to test this.
Firstly, you have used the wrong brackets for $lang = $this->input->post['lang'];
so it just isn't happy, but doesn't throw an error, no idea why.
You should change it to:
$lang = $this->input->post('lang');
Note the curved brackets.
Secondly, I'm not sure if it's intentional or not but you actually have 2 different $data
variables going on:
$this->data
and $data
. So echo json_encode($data, true);
shows only what it contains, perhaps you meant echo json_encode($this->data, true);
Finally, the code snippet above is missing a final closing curly brace, hopefully it's not missing from the source!
I hope this helps solve your problem =)
try return json_output:
public function test(){
$lang= $this->input->post['lang'];
if($lang=="ar"){
$message= 'Arabic test';
return parent::json_output(['code' => '1', 'message' => 'Succesful', 'data' => $message]);
}
//else if
// else if($lang=="en")
else{
$message= 'English test';
return parent::json_output(['code' => '2', 'message' => 'Succesful', 'data' => $message]);
}
$data['status']= 1 ;
echo json_encode($data,true);
die;
}