dongwh1992 2014-02-14 14:37
浏览 85

为什么jquery中的$ .each无法读取jquery中的'data'?

I have this listener who listens in this php function

public function notification_query(){

$this->sse();
$data['company_id'] = $this->session->userdata('companyId');
$data['chikito']=$this->pm_model->notifications_admin($data);

echo "data: ".json_encode($data['chikito'])."

";
ob_flush();
flush();
sleep(1); }

and if the server has an event, it will pass automatically to the client side script function.

var es = new EventSource("<?php echo base_url(); ?>admin/notification_query");

 var listener = function (data)
 {
    var data = data.data;
    //$("#notification").append(data);   
      $.each(data, function(index, val) {  
        $("#notification").append(val.details); 
       });

 }

  es.addEventListener("message", listener);

now the problem is, why can't the $.each in jquery can't read the data pass from the server? It didn't print. I have no idea why.

By the way, if I use $("#notification").append(data); It will print the data that is pass.

thanks in advance for those who answers.

  • 写回答

1条回答 默认 最新

  • douba9020 2014-02-14 14:52
    关注

    The JSON.stringify() method converts a value to JSON.

    var data = JSON.stringify(data).data;
    
    JSON.stringify({});                  // '{}'
    JSON.stringify(true);                // 'true'
    JSON.stringify("foo");               // '"foo"'
    JSON.stringify([1, "false", false]); // '[1,"false",false]'
    JSON.stringify({ x: 5 });            // '{"x":5}'
    JSON.stringify({x: 5, y: 6});        // '{"x":5,"y":6}' or '{"y":6,"x":5}'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序