网页内容未通过 JSON 函数(数据)更新


webpage content not updated through json function(data)

在数据库中插入一些数据并返回 json 对象后,我正在尝试动态更新网页,但无济于事。

假设我有

<div id="try1"> try(<span id="votes1">0</span>)</div>

当页面显示当前票数时,(0)当前

然后我有一个按钮

<button  onclick="vote(1);">+</button>

调用此函数:

function vote(votes_id)
{
  var div = 'try' +votes_id;
  var url = 'vote.php?id=' +votes_id;
  var destination = '#votes' +votes_id;
  $.getJSON( url, function(data) {
    $(destination).html (data.votes);
  }); 
}

我的投票.php返回此 JSON 或在正确更新数据库后返回,或者,至少我是这么认为

{"votes":"1"}

但是我的网页没有从 0 更新到 1,

我使用$data = json_encode($data);

其结果var_dump为:

string '{"votes":"1"}'

{"votes":"1"}

作为回声$data。

我可能错过了什么?

那是

因为你把他的成功回调放在了错误的地方。

用:

function vote(votes_id)
{
  var div = 'try' +votes_id;
  var url = 'vote.php?id=' +votes_id;
  var destination = '#votes' +votes_id;
  $.getJSON( url, {}, function(data) {
    $(destination).html (data.votes);
  }); 
}