住基于数据throuhg PHP文件AJAX数据数据、文件、PHP、throuhg

2023-09-10 21:32:01 作者:那年初夏

这是我的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_de code,只是把VAL就像是对测试)

and here is the ajax.php file ( i didnt put json_decode and just put the val just like that for testing)

{"price":"o"}

什么即时试图做的是,如果我去ajax.php文件并更改邻为别的东西,我想要的数据自动更新,并显示索引页面上没有刷新,但我似乎无法得到它的工作。任何人看到什么即时做错了什么?

what im 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 cant seem to get it to work. anyone see what im doing wrong?

推荐答案

客户端没有办法知道的东西在服务器上已经改变了。所以,你可以用定期AJAX请求与的setInterval 功能:

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 。