我使用代码
从CodeIgniter控制器向Ajax发送数据和视图$this->load->model('query_mainmodel');
$data['result'] = $this->query_mainmodel->getcategories();
print $this->load->view('add_content',$data,true);
在Ajax中我有
$.ajax({
url: url,
type: 'POST',
dataType: "text",
success: function (response) {
tabID.html(response); //now this time response contains html only
}
});
我得到了回复的HTML,如何从ajax获取数据?
答案 0 :(得分:2)
你说你可以获得HTML,但现在你需要数据。因此,在您的控制器中,您可以将视图和数据作为json传递回来:
$data['result'] = $this->query_mainmodel->getcategories();
echo json_encode( array(
'view_html' => $this->load->view('add_content',$data,true),
'data' => $data['result']
));
然后在你的JS中:
$.ajax({
url: url,
type: 'POST',
dataType: "json", // <- make sure to change this
success: function (response) {
// view HTML available like this
console.log( response.view_html );
// data available like this
console.log( response.data );
}
});