Codeigniter 在每次输出前打印一个奇怪的字符
Codeigniter prints a strange character before every output
我在 PHP codeigniter 框架中制作了一个 API,returns JSON 输出。输出看起来不错,但我无法解析它:它总是给出解析错误。
在使用在线 JSON 解析器进行解析时,我意识到所有 API 输出的开头都有一个奇怪的特殊字符,无论我是 return JSON 还是一个简单的字符串。 ( 见下文 )
this is the link to my API and I tested the output on JSLint
你能帮我看看开头这个字符是从哪里来的吗?
这是我的控制器代码:
public function getCleaner(){
$mobile_no = $this->input->get('mobile');
$pass = $this->input->get('pass');
if(!is_null($mobile_no) && !is_null($pass))
{
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
header('x-xss-protection: 1;');
$data = $this->cleaner_model->get_cleaner($mobile_no, $pass);
if(!empty($data))
echo json_encode($data);
else
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
}
}
这是我的模型代码:
public function get_cleaner($mobile, $pass){
$this->db->select("*");
$this->db->from("cleaner");
$this->db->where("mobile1", $mobile);
$this->db->where("user_pass", $pass);
$data = $this->db->get()->row_array();
return $data;
}
您用来创建 JSON 的方法不正确。使用以下内容获得正确的输出(在您的控制器的 ELSE 块中)
//this is WRONG
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
//this is correct
$arr = array('cleaner_id' => '-10', 'cleaner_name' => 'cleaner_not_found');
echo json_encode($arr);
现在您将收到正确的数据
{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}
我在 PHP codeigniter 框架中制作了一个 API,returns JSON 输出。输出看起来不错,但我无法解析它:它总是给出解析错误。
在使用在线 JSON 解析器进行解析时,我意识到所有 API 输出的开头都有一个奇怪的特殊字符,无论我是 return JSON 还是一个简单的字符串。 ( 见下文 )
this is the link to my API and I tested the output on JSLint
你能帮我看看开头这个字符是从哪里来的吗?
这是我的控制器代码:
public function getCleaner(){
$mobile_no = $this->input->get('mobile');
$pass = $this->input->get('pass');
if(!is_null($mobile_no) && !is_null($pass))
{
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
header('x-xss-protection: 1;');
$data = $this->cleaner_model->get_cleaner($mobile_no, $pass);
if(!empty($data))
echo json_encode($data);
else
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
}
}
这是我的模型代码:
public function get_cleaner($mobile, $pass){
$this->db->select("*");
$this->db->from("cleaner");
$this->db->where("mobile1", $mobile);
$this->db->where("user_pass", $pass);
$data = $this->db->get()->row_array();
return $data;
}
您用来创建 JSON 的方法不正确。使用以下内容获得正确的输出(在您的控制器的 ELSE 块中)
//this is WRONG
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
//this is correct
$arr = array('cleaner_id' => '-10', 'cleaner_name' => 'cleaner_not_found');
echo json_encode($arr);
现在您将收到正确的数据
{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}