开发者

live ajax data based on data through php file

开发者 https://www.devze.com 2023-03-04 03:22 出处:网络
here is my index.html file $(document).ready(function() { $.ajax({ url: \'ajax.php\', type: \'GET\', dataType: \"json\",

here is my index.html file

$(document).ready(function() {

    $.ajax({
        url: 'ajax.php',
        type: 'GET',
        dataType: "json",

        beforeSend: function() 
    开发者_Go百科    {

        },
        complete: function() 
        {

        },
        success: function(result) 
        {

        $("p").html(result.price);

        $("p").live("load", function() {
            $(this).html(result.price);
        });
        }
    });
});

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"}

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?


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);

Another possibility is to use push AJAX.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号