weixin_33738578 2019-07-14 09:41 采纳率: 0%
浏览 74

用ajax更新一个div

I'm trying to update an h3 with the id data using Ajax. The Ajax uses a get request to retrive data from an API, but for some reason the html is not being updated.

This is what the json data looks like {ticker: "TEST", Price: 7876}

I tried to change $('#data').text(data[0].data[]); to $('#data').text(data[ticker].data[]);, for example, but it didn't work either.

I added a console.log(data[0]) to debug it, and the data normally appears in my console.

function doPoll(){

    $.get('http://localhost:8000/tst/', function(data) {
        console.log(data[0]);
        $('#data').text(data[0].data);
        setTimeout(doPoll, 1000);
    });
} 

doPoll();

And this is the html part to be updated:

<h3 id="data"></h3>

Basically instead of that h3 there should be my data updated every tot seconds, but at the actual moment nothing appears.

  • 写回答

4条回答 默认 最新

  • H_MZ 2019-07-14 09:50
    关注

    To access the ajax response you need to parse that response data.

    For e.g: data=JSON.parse(data);

    Now you can you use this data to change the text of <h3>

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题