我试图从php代码访问输出到jquery ajax。但我不知道为什么它会返回包含结果的整个页面html。任何人都可以告诉我这件事。在Firefox的控制台它显示我的页面html包括PHP结果的响应。但在jQuery代码console.log没有命中。无法从ajax请求中获取来自php返回的响应
这里是jQuery代码
function getprofile()
{
$.ajax({
url: 'Userpage/get_profile',
//data: {'title': title}, change this to send js object
type: "post",
dataType: 'json',
data: {'userid': 1},
success: function(data) {
console.log(data);
}
});
}
我的PHP代码
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Userpage extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library("session");
$this->load->model("Userpage_model");
$this->load->view('Userpage_view');
}
public function index()
{
}
public function get_profile()
{
$data = array();
$post_id = $this->input->post('userid');
$data['comments'] = $this->Userpage_model->get_profile($post_id);
echo json_encode($data['comments']);
exit;
}
}
?>
请检查代码,并告诉我在哪里,我错了
感谢
请显示更多/你所有的php代码! –
请立即检查@low_rents –
您使用的是什么框架?我不明白你的ajax调用url:'Userpage/get_profile''如何导致'Userpage'类中的get_profile()方法执行。 –