我試圖從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()方法執行。 –