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)
    }
});

出于某种原因,这段代码返回所需的 PHP,但它也返回我的 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 */
}

最后,我的头.php包含你常用的 <head> 标签,其中包含 javascript 和 css 导入。

输出如下所示:

**** return from php *****<head>header stuff</head>

我宁愿不解析标头信息,而只是获取 PHP 输出。

注意:我正在使用codeiginiter,并且正在front_office控制器中调用一个函数。

第二点:我知道我现在没有发布任何东西,但我很快就会发布。我试过GET但问题仍然存在。

您正在返回视图,检查请求,如果它不是 ajax,则加载视图,否则将 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));
}

有关返回 JSON 的更多信息,请访问:从 PHP 脚本返回 JSON

另外,检查您是否在构造函数方法中加载头.php?请从控制器发布整个代码。