我搜索了很多问题,但不适合我,我想循环通过一个控制器返回的json对象,让我给你看代码:
public function traer_pasajero($id){
$this->db->select('*');
$this->db->from('pasajeros');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result();
}
控制器public function traer_pasajero(){
$pasajero = $this->pasajeros_model->traer_pasajero($this->input->post('id'));
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($pasajero));
return $pasajero;
}
JS
function modificarPasajero (id) {
$.ajax({
type: "POST",
url: "http://localhost/xxx/xxx/traer_pasajero",
data: {id: id},
dataType: "text",
cache:false,
success:
function(data){
$.each( data, function( key, value ) {
alert(value);
});
}
});
}
我在firebug中收到这个错误:类型错误:无效的'in'操作数
这是我在firebug中看到的json对象:
[{"id":"1","primer_nombre":"xxxx"、"segundo_nombre":","primer_apellido":"xxxx"、"segundo_apellido":","词":"xxx"、"nacionalidad":"Uruguayo"、"fecha_nacimiento":"5/7/1910"、"vencimiento":"5/5/1910"、"asiento":"12","habitacion":"西格里碳素集团","莱万特":","电话号码":","observaciones":","id_envio":"2"}]
dataType
应该是"json"
而不是"text"
,并且在每个循环之前尝试console.log(data)
尝试删除此语句
return $pasajero;
,因为它正在覆盖你的json..可以简单地返回
return json_encode($pasajero);
,然后尝试还cahnge
datatype : "json"
现在可以使用eval(string)或JSON.parse(string)进行迭代。但是注意eval是危险的
var obj = eval("("+data+")");
或
var obj = parseJSON(data);
$.each( obj, function( key, value ) {
alert(value);
});