저는 Codeigniter와 Jquery ajax를 사용하여 mysql에서 데이터를 호출하고 html 형식으로 보냅니다.
문제 : res 개체 에서 데이터를 호출하는 방법을 모르겠습니다 .
여기 내 컨트롤러입니다
public function select_post(){
$data = $this->ads_m->get_post('ads',array('user_id'=> $this->user->user_id()));
IF($data){
foreach($data as $row ){
$response = array(
'name'=>$row->name,
'price'=>$row->price,
'addr'=>$row->des,
'img'=>$row->img,
'status'=>$row->status,
);
echo json_encode(array('res'=>$response));
}
}else{
echo json_encode(array('res'=>FALSE));
}
}
그리고 여기에 모델 함수가 있습니다.
public function get_post($from, $where = False) {
$this->db->select("*");
$this->db->from($from);
if ($where) {
$this->db->where($where);
}
$q = $this->db->get();
if($q->num_rows()>0){
return $q->result();
}
}
그리고 이것은 Ajax
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '<?php echo base_url('ads/select_post'); ?>',
data: {},
dataType: "json",
cache: false,
success: function (data) {
console.log(data.['res']['name']);
}
});
});
그리고 나는 아래와 같은 결과를 얻습니다.
{"res":{"name":"dfasdfas","price":"0","addr":"sadfdfasdfasdf","img":"","status":"1"}}{"res":{"name":"Heng"
,"price":"0","addr":"asdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasd
fasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdf"
,"img":"","status":"1"}}{"res":{"name":"asdDasdA","price":"0","addr":"ASDasdASD","img":"","status":"1"
}}{"res":{"name":"asdfas","price":"0","addr":"dfasdf","img":"","status":"1"}}
// 먼저 응답을 변경합니다. public function select_post () {
$data = $this->ads_m->get_post('ads',array('user_id'=> $this->user->user_id()));
IF($data){
var $responseArray = [];
foreach($data as $row ){
$response = array(
'name'=>$row->name,
'price'=>$row->price,
'addr'=>$row->des,
'img'=>$row->img,
'status'=>$row->status,
);
array_push($responseArray, $response);
}
echo json_encode($responseArray);
}else{
echo json_encode(array('res'=>FALSE));
}
}
//Then used below code to get the response in ajax
$(document).ready(function () {
$.ajax({
url: '<?php echo base_url('ads/select_post'); ?>',
data: {},
dataType: "json",
cache: false,
success: function (data) {
$.each(data, function(item) {
console.log(item.name);
});
}
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다