Javascript 使用jquery在json codeigniter对象中循环

Javascript 使用jquery在json codeigniter对象中循环,javascript,php,jquery,json,codeigniter,Javascript,Php,Jquery,Json,Codeigniter,我搜索了很多问题,但对我来说不起作用,我想通过控制器返回的json对象进行循环,让我向您展示代码: 型号 public function traer_pasajero($id){ $this->db->select('*'); $this->db->from('pasajeros'); $this->db->where('id', $id); $query = $this->db->get(); return

我搜索了很多问题,但对我来说不起作用,我想通过控制器返回的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

功能修改arpasajero(id){

}

我在firebug中收到这个错误: TypeError:操作数a中的“in”无效

这是我在firebug中看到的json对象:


[{“id”:“1”,“primer_nombre”:“xxxx”,“segundo_nombre”:“primer_apellido”:“xxxx”,“segundo_apellido”:“ci”:“xxx”,“Nationalidad”:“Uruguayo”,“fecha_Naciminto”:“5/7/1910”,“vencimiento”:“5/5/1910”,“asiento”:“12”,“habitacion”:“Sgl”,“levante”:“telefono”:“观察者”:“id_Enviro”:“2”

试着删除这句话

 return $pasajero;
因为它正在覆盖您的json。。 你可以直接返回

 return json_encode($pasajero);
然后试试看 还有卡恩奇

datatype : "json"
现在可以使用eval(字符串)或JSON.parse(字符串)进行迭代。但请注意,eval是危险的

   var obj = eval("("+data+")"); 


dataType
应该是
“json”
而不是
“text”
,并在每个循环之前尝试
console.log(data)

[Object{id=“1”,primer_nombre=“xxx”,primer_apellido=“xxx”,más…]向我展示了这一点,但我可以如何迭代?好的,json响应看起来不错。迭代它,就像您已经尝试使用$.each(数据、函数(键、值){…})一样;我在each和return[object object]中设置了alert(value),可以吗?是的,这不是alert()函数的预期行为。如果要查看数据,请尝试console.log('Key:'+Key+',Value:'+Value);在这一点上,您应该对该数据进行一些处理,例如向用户显示数据或其他任何操作。检查jQuery函数,如$(selector).html()和$(selector).append(),例如…我使用$.each(数据、函数(索引、元素){alert(element.primer_nombre);$('primer_nombre').val(element.primer_nombre)});还有工作!parseJson未定义JSON中的parseJson始终大写您也可以使用eval one,因为您知道要发送什么,但如果您不知道,请避免eval,在这种情况下可以:)
   var obj = eval("("+data+")"); 
   var obj = parseJSON(data);  
  $.each( obj, function( key, value ) {
        alert(value);
        });