[Object {id="1", name="Muslin", image1="muslin.jpg"}, Object {id="72", name="Gagra choli", ...}]
这是我在 php 中使用 json_encode 得到的 json 输出。当我实现它时,它说无效的json,为什么?
我用这个实现了json
编辑:这是控制器:
function show_data()
{
$this->load->model("elements_model");
echo json_encode($this->elements_model->get_all());
}
这是模型:
<?php
class Elements_model extends CI_Model
{
function __construct(){
parent::__construct();
}
public function insert_elements($data)
{
return $this->db->insert('elements',$data);
}
public function get_all()
{
$sql = 'SELECT * FROM elements';
$query = $this->db->query($sql);
// Fetch the result array from the result object and return it
return $query->result();
}
}
这是视图
<html>
<head>
<title> ajax call first time </title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
//alert("hello");
$.ajax({
type:'POST',
dataType: "json",
url:'elements/show_data',
success:function(res){
console.log(res);
//var obj=$.parseJSON(res);
$('#response').html("obj.name");
//$('#response').show();
},
error:function(xhr,status,error)
{
$('#response').html(xhr.responseText);
}
});
});
</script>
</head>
<body>
<div class="response" id="response" ></div>
<div class="container" id="container" ></div>
</body>
这是console.log
[{"id":"1","name":"Muslin","image1":"muslin.jpg"},{"id":"72","name":"Gagra choli","image1":"gagra choli.jpg"},{"id":"73","name":"Lahenga saree","image1":"lahenga saree.jpg"},{"id":"74","name":"Ao dai","image1":"Ao dai.jpg"},{"id":"75","name":"Brocade","image1":"brocade.jpg"},{"id":"77","name":"Button","image1":"button.jpg"},{"id":"78","name":"Bathrobe","image1":"Bathrobe.jpg"},{"id":"79","name":"Bathtowel","image1":"Bathtowel.jpg"},{"id":"80","name":"Cassock","image1":"cassock.jpg"},{"id":"81","name":"Shirt","image1":"shirt.jpg"},{"id":"82","name":"Dolce and Gabanna","image1":"dolce_and_gabanna.jpg"},{"id":"83","name":"asasa","image1":"1009578_1392050054339713_1292927121_o.jpg"},{"id":"84","name":"knbdfjhbjhbvjh","image1":"images_(1)10.jpg"}]
我更新了 console.log