droc60607 2015-07-16 05:14
浏览 27

用ajax json代码获取php结果

Hello this is my code to display data each 2s without refresh page but I don't know why it's not working.

<body>
    <script type="text/javascript">
    $(document).ready(function() {
        done();
    });

    function done() {
        setTimeout(function() {
            updates();
            done();
        }, 2000);
    }

    function updates() {
        $.getJSON("saipa.php", function(data) {
                $("ul").empty();
                $.each(data.result, function(){
                    $("ul").append("<li>Name: "+this['Name']+"</li>
                         <li>detail: "+this['Description']+"</li>
                        <li>Today price: "+this['Today']+"</li>
                         <li>Last day price: "+this['Lastday']+"</li>
                        <br />");
                 });
             });
        }
    </script>
    <ul></ul>
</body>

JSON:

[{"Name":"arash","Description":"vian","Today":"20,500,000","Lastday":"22,410,000"},{"Name":"shaber","Description":"root","Today":"38,200,000","Lastday":"40,210,000"}]
  • 写回答

2条回答 默认 最新

  • doutang1946 2015-07-16 05:22
    关注

    JSON cannot parse with [] use . operator Try this code

    function updates() {
         $.getJSON("saipa.php", function(data) {
           $("ul").empty();
           $.each(data, function(i,value){
            $("ul").append("<li>Name: "+value.Name+"</li>
                                 <li>detail: "+value.Description+"</li>
                                <li>Today price: "+value.Today+"</li>
                                <li>Last day price: "+value.Lastday+"</li>
                                <br />");
           });
     });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来