AJAX 返回 PHP 和 head.php(页面源)
AJAX Returning PHP and head.php (page source)
我这里有一些 ajax:
$.ajax({
type: "POST",
url: "<?=site_url('front_office/get_email_list/')?>",
dataType: "text",
success: function(response) {
console.log(response)
}
});
出于某种原因,此代码 returns 所需的 PHP 但它也是 returns 我的 head.php 文件。
function get_email_list() {
$center_ids = array(
/* list of user ids */
);
print_r(json_encode($this->user_model->get_email_list($center_ids)));
/* get_email_list($center_ids)) returns a database query result */
}
最后,我的 head.php 包含您通常使用 javascript 和 css 导入的 <head>
标签。
输出类似于:
**** return from php *****<head>header stuff</head>
我宁愿不解析 header 信息,而只是获取 PHP 输出。
注意:我正在使用 codeigniter 并且我在 front_office
控制器中调用一个函数。
第二个注意事项:我知道我现在不会发布任何内容,但我很快就会发布。我尝试了 GET
但问题仍然存在。
您正在 return 查看,检查请求,如果不是 ajax,加载视图,否则 return json 编码结果到您的 ajax 请求。
if (!$this->input->is_ajax_request()) {
// load view
}else{
// Adding header, so jQuery ajax request will know that it is json
// and result will be parsed immediately
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($email_list_result));
}
有关 returning JSON 的更多信息,请访问:
Returning JSON from a PHP Script
此外,请检查您是否在构造方法中加载 head.php?
请 post 来自控制器的完整代码。
我这里有一些 ajax:
$.ajax({
type: "POST",
url: "<?=site_url('front_office/get_email_list/')?>",
dataType: "text",
success: function(response) {
console.log(response)
}
});
出于某种原因,此代码 returns 所需的 PHP 但它也是 returns 我的 head.php 文件。
function get_email_list() {
$center_ids = array(
/* list of user ids */
);
print_r(json_encode($this->user_model->get_email_list($center_ids)));
/* get_email_list($center_ids)) returns a database query result */
}
最后,我的 head.php 包含您通常使用 javascript 和 css 导入的 <head>
标签。
输出类似于:
**** return from php *****<head>header stuff</head>
我宁愿不解析 header 信息,而只是获取 PHP 输出。
注意:我正在使用 codeigniter 并且我在 front_office
控制器中调用一个函数。
第二个注意事项:我知道我现在不会发布任何内容,但我很快就会发布。我尝试了 GET
但问题仍然存在。
您正在 return 查看,检查请求,如果不是 ajax,加载视图,否则 return json 编码结果到您的 ajax 请求。
if (!$this->input->is_ajax_request()) {
// load view
}else{
// Adding header, so jQuery ajax request will know that it is json
// and result will be parsed immediately
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($email_list_result));
}
有关 returning JSON 的更多信息,请访问: Returning JSON from a PHP Script
此外,请检查您是否在构造方法中加载 head.php? 请 post 来自控制器的完整代码。