2011-05-07 57 views
1

這裏是我的index.html文件現場AJAX數據

$(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就像是對測試)

{"price":"o"} 

什麼即時試圖做的是,如果我去到ajax.php文件,改變O至別的東西,我希望將數據自動更新,並顯示索引頁上沒有刷新,但我不能似乎讓它工作。任何人看到我做錯了什麼?

+0

你可以使用螢火蟲和檢查網絡面板如何請求 – kobe 2011-05-07 06:46:27

+0

在這裏你去我檢查它,但你也可以看到。我改變了php文件,顯示一個隨機數,像蘭特(0,10000)http://69.231.223.112:8888/test/ – 2011-05-07 06:49:01

+0

@same,你會感覺到對嗎?有什麼問題然後 – kobe 2011-05-07 07:00:50

回答

3

客戶端無法知道服務器上的某些內容已更改。所以,你可以定期使用AJAX請求與setInterval功能:

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

另一種可能性是使用push AJAX

+0

他的代碼中有什麼問題?我沒有得到他在問什麼 – kobe 2011-05-07 07:02:57

+0

@kobe,他想修改服務器上的'ajax.php'文件發送的JSON響應,並期望客戶端使用新值進行更新。 – 2011-05-07 07:03:50

+0

謝謝,是live()與此相同還是它有不同的目的? – 2011-05-07 07:37:33