通过PHP文件基于数据的实时ajax数据 [英] live ajax data based on data through php file
问题描述
这是我的index.html文件
here is my index.html file
$(document).ready(function() {
$.ajax({
url: 'ajax.php',
type: 'GET',
dataType: "json",
beforeSend: function()
{
},
complete: function()
{
},
success: function(result)
{
$("p").html(result.price);
$("p").live("load", function() {
$(this).html(result.price);
});
}
});
});
这是ajax.php文件(我没有放json_decode,而只是将val放进去进行测试)
and here is the ajax.php file (I didn't put json_decode and just put the val just like that for testing)
{"price":"o"}
我想做的是,如果我转到ajax.php文件并将o更改为其他内容,我希望数据自动更新并显示在索引页面上而无需刷新,但我似乎无法得到它的工作.我在做什么错了?
what I'm trying to do is if I go to the ajax.php file and change the o to something else I want the data to automatically update and display on the index page without a refresh but I can't seem to get it to work. What am I doing wrong?
推荐答案
客户端无法知道服务器上的某些内容已更改.因此,您可以通过 setInterval 函数使用定期AJAX请求:
The client has no way of knowing that something on the server has changed. So you could use periodic AJAX requests with the setInterval function:
window.setInterval(function() {
// Every 5 seconds send an AJAX request and update the price
$.ajax({
url: 'ajax.php',
type: 'GET',
dataType: 'json',
cache: false,
success: function(result) {
$('p').html(result.price);
}
});
}, 5000);
另一种可能性是使用按AJAX .
这篇关于通过PHP文件基于数据的实时ajax数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!