Codeigniter 启用CI分析器会导致json解析错误
我有CI profiler,就像这样: $this->output->enable_profiler(真) 在我的控制器里 这导致JSON.parse出现意外的非空白字符错误。当我关闭探查器时,一切正常 我发现ajax响应文本也有分析器信息 有人能告诉我这一点吗。 如何在CI中打开探查器并避免jason解析错误。Codeigniter 启用CI分析器会导致json解析错误,codeigniter,Codeigniter,我有CI profiler,就像这样: $this->output->enable_profiler(真) 在我的控制器里 这导致JSON.parse出现意外的非空白字符错误。当我关闭探查器时,一切正常 我发现ajax响应文本也有分析器信息 有人能告诉我这一点吗。 如何在CI中打开探查器并避免jason解析错误。 thanx预先我在我的核心控制器类中这样做(扩展CI_控制器,是我所有应用程序控制器的父类): 你也可以说: $this->output->enable_profiler(
thanx预先我在我的核心控制器类中这样做(扩展CI_控制器,是我所有应用程序控制器的父类): 你也可以说:
$this->output->enable_profiler(FALSE);
在您的ajax控制器方法中,这样该方法就不包括探查器数据/字符串。首先,是的,我知道这个问题已经存在了4年,但从2013年到2017年,ajax调用在我们的网站/web系统中变得更为流行,没有服务器端性能方面的反馈对我来说不是一个选择,下面是我所做的: application\core\MY_Controller.php:(将探查器加载为库)
只要那行代码存在就行……但在返回JSON的Ajax请求中不需要它。
$this->output->enable_profiler(FALSE);
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
if(ENVIRONMENT === 'development'){
$this->load->library('profiler'); //(don't use: $this->output->enable_profiler(FALSE), use it as a library!)
}
}
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Somecontroller extends MY_Controller {
function ajaxCall()
{
// Do some stuff
$ret = array(
'something' => 'yada yada yada',
'_profiler' => $this->profiler->run()
);
header("Content-type:application/json");
return print(json_encode($ret));
}
}
<script>
var ajaxReturn; // Your prefered ajax call (native JS or jQuery or wathever)
if ( typeof ajaxReturn === 'string') {
ajaxReturn = JSON.parse(ajaxReturn);
}
//-- Do some stuff
//-- Append the profiler output
document.getElementByTagName('body')[0].append(ajaxReturn._profiler); //-- or jQuery: $('body').append(ajaxReturn._profiler);
</script>