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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题