0

I have a jsp page that is refreshing periodically after 5 second to display latest data. In this page I am calling js files . Using jquery ajax refresh i am able to refresh my server side coding but it is not affecting on the page. I guess this is because my java scripts are not getting refreshed periodically.

Please tell me how to call javascript after jquery ajax refresh.

Below is the code i am using for the jquery java script refresh

<script>
            (function worker() {
                $.ajax({
                    url: 'http://localhost:8088Login.do', 
                    success: function(data) {
                       // $("#refresh").html(data);
                        // alert(data);
                    },
                    complete: function() {

                        //alert("refresh");
                        // 
                        // Schedule the next request when the current one's complete
                        setTimeout(worker, 5000);
                    }
                });
            })();
        </script>
Annapoorni D
  • 831
  • 2
  • 13
  • 30
Sudarshan kumar
  • 1,503
  • 4
  • 36
  • 83
  • do you get alert if you un-comment `alert("refresh");`? do you try this : `setTimeout(function(){ worker(); },5000);` ? – dann Oct 11 '13 at 06:19
  • setTimeout(function(){ worker(); },5000); this also i have tried alert is printing server side rfresh is also happening but data not getting refreshed on the page. – Sudarshan kumar Oct 11 '13 at 06:31

2 Answers2

0

does the solution require javascript? you can achieve the same result using html meta tag refresh.

<meta http-equiv="refresh" content="5">

example: http://plnkr.co/edit/F8ITSl?p=preview

rickhuynh
  • 1,176
  • 1
  • 8
  • 5
  • Hi ..I have already tried it but each time whole page refreshes but i need only data to be refreshed .without any loading symbol.I mean data should not blink – Sudarshan kumar Oct 11 '13 at 05:25
  • right, it's because the meta tag causes your browser to periodically refresh the entire page. regarding your javascript ajax issue, like what Adrian mentioned, your url seems to be incorrect. i think it should be http://localhost:8088/Login.do. Make that change, and then uncomment `$("#refresh").html(data)`. if data is being returned, you see your #refresh tag being updated. – rickhuynh Oct 23 '13 at 18:27
  • so your code should be like: (function worker() { $.ajax({ url: 'http://localhost:8088/Login.do', success: function(data) { $("#refresh").html(data); // alert(data); }, complete: function() { //alert("refresh"); // // Schedule the next request when the current one's complete setTimeout(worker, 5000); } }); })();` – rickhuynh Oct 23 '13 at 18:33
  • I have tried that but page refreshes but java script does not refresh ..So values are not appearing .. – Sudarshan kumar Oct 25 '13 at 05:05
0

You are doing an AJAX request every 5 seconds. The url seems to me to not be right. In JSP, you either make an AJAX request to a servlet, to a JSP page, or to some other resource. I don't know what .do is.

Check out BalusC explanation here link

You do not fully grasp what and how AJAX works. You should learn more about this.

Here is a video tutorial

Community
  • 1
  • 1
Adrian Stamin
  • 687
  • 2
  • 8
  • 21