最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get realtime data using ajax - Stack Overflow

programmeradmin1浏览0评论

Hello guys i am trying to show real time loop data. using AJAX i want when AJAX run its get real time number update from action page loop.

$.ajax({
            url: "number.php",
            type: "POST",
            data: {
                name: name
            },
            cache: false,
            success: function() {
            /// some code to get result
           }

and loop page number.php

<?php
$id =$_POST['id'];
 while(true) // no matter
    { 
echo '<script>
        int i = 0;
        int ii += i;
        print(i); // i want to shoe this value 
        i++;
      </script>'
 }

Hello guys i am trying to show real time loop data. using AJAX i want when AJAX run its get real time number update from action page loop.

$.ajax({
            url: "number.php",
            type: "POST",
            data: {
                name: name
            },
            cache: false,
            success: function() {
            /// some code to get result
           }

and loop page number.php

<?php
$id =$_POST['id'];
 while(true) // no matter
    { 
echo '<script>
        int i = 0;
        int ii += i;
        print(i); // i want to shoe this value 
        i++;
      </script>'
 }
Share Improve this question edited Mar 24, 2017 at 11:29 DroidNoob 1,1439 silver badges21 bronze badges asked Mar 24, 2017 at 9:25 bakuxirehabakuxireha 211 gold badge1 silver badge1 bronze badge 3
  • 4 A newer approach to this is to use sockets, see socket.io – Uberswe Commented Mar 24, 2017 at 9:28
  • 2 You probably want to try WebSockets instead. AJAX is not designed for the kind of the data exchange you are trying to achieve. – Alex Pavlenko Commented Mar 24, 2017 at 9:28
  • You're posting name and retrieving $_POST['id']? The question very unclear about what it is you're trying to do. I don't see why you would need to involve PHP at all, when looking at your examples. – M. Eriksson Commented Mar 24, 2017 at 9:33
Add a ment  | 

1 Answer 1

Reset to default 4

What you are trying to do can be achieved by using web-sockets or http-long-polling

I would suggest that you use websockets, using Ratchet, Ratchet is a loosely coupled PHP library providing developers with tools to create real time, bi-directional applications between clients and servers over Web-sockets.

Using ajax is kinda not a good idea, because you have to send request to the server after every X seconds, to fetch data even if there are no changes in the server. doing this is more like you are DDos attacking your own server without knowing that.

But if you insist on ajax you would run a function that will send a request to the php script every x seconds, here's how you would do that with Ajax.

$('document').ready(function () {
 setInterval(function () {getRealData()}, 1000);//request every x seconds

 }); 

function getRealData() {
$.ajax({
         url: "number.php",
         type: "POST",
         data: {
             name: name
         },
         cache: false,
         success: function () {
             /// some code to get result
         }
     }
 }

NB: AS I have said above better have a look into websockets.

发布评论

评论列表(0)

  1. 暂无评论