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>

    评论

报告相同问题?