我有CI分析器就像這樣: $ this-> output-> enable_profiler(TRUE) 在我的控制器中。啓用CI分析器給出json解析錯誤
這是給JSON.parse:意外的非空格字符錯誤。當我關閉它的探查器時就OK了。
我發現Ajax響應文本有剖析的信息也是如此。
有人可以啓發我這個。 如何在CI上打開剖析器並避免jason解析錯誤。 thanx提前
我有CI分析器就像這樣: $ this-> output-> enable_profiler(TRUE) 在我的控制器中。啓用CI分析器給出json解析錯誤
這是給JSON.parse:意外的非空格字符錯誤。當我關閉它的探查器時就OK了。
我發現Ajax響應文本有剖析的信息也是如此。
有人可以啓發我這個。 如何在CI上打開剖析器並避免jason解析錯誤。 thanx提前
我不喜歡這樣的我的核心控制器類(擴展是CI_Controller是父類我所有的應用程序控制器):
if(!$this->input->is_ajax_request()) {
$this->output->enable_profiler(TRUE);
}
你也可以把:
$this->output->enable_profiler(FALSE);
在您的ajax控制器方法中,該方法不包含profiler數據/字符串。
首先,是的,我知道這個問題爲+4歲,但2013至17年的Ajax調用了更受歡迎的在我們的網站/網絡系統沒有在服務器端性能的一些反饋是不是我的選擇,所以這裏是我做了什麼:
應用程序\核心\ MY_Controller.php:(負載探查如圖書館)
<?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!)
}
}
}
何時何地您需要它得到的輸出只作爲:(請記住擴展MY_Controller而不是CI_Controller)
<?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));
}
}
現在你的Javascript中追加探查輸出,而不破壞你的JSON結構:
<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>
最後說明:
當然,這種形式給出將polute與剖析廣泛的HTML你的Ajax調用,所以切勿將此分析器保留在PRODUCTION中,甚至在開發時只使用它來分析您的應用程序,否則如果沒有它,情況可能會更好。
希望它可以幫助別人,因爲它對我有用。
那麼profiler將不可用於ajax請求? – 2013-04-24 19:36:35
只要那行代碼存在就行了......但是您不希望它在返回JSON的Ajax請求中。 – jcorry 2013-04-24 19:40:21