1

Below is the JS code snippet for AJAX call every 1 sec to fetch the value of session attribute and print on UI

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
       setInterval(function ()
        {
        <%
            HttpSession session = request.getSession(false);
            if (session != null && session.getAttribute("invalidMaskedMSISDNCount") != null) {
                %>var invalidMaskedMSISDNCount = "<%=session.getAttribute("invalidMaskedMSISDNCount")%>" ;
        <% }
        %>
           $('#failedToMaskMSISDNCount').text(
                        'The count of MSISDN failed to mask is '
                                + invalidMaskedMSISDNCount);
        },1000);

    });
</script>

The variables gets updated only on refresh page. However below code snippet to calculate random number on client side and print on UI on AJAX call every 1 sec works perfectly.

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
   $(document).ready(
        function() {
            setInterval(function() {
                var randomnumber = Math.floor(Math.random() * 100);
                $('#show').text(
                        'I am getting refreshed every 1 seconds..! Random Number ==> '
                                + randomnumber);
            }, 1000);
        });

Below is the div for above two calls.

<div id="failedToMaskMSISDNCount" align="center"></div>
 <div id="show" align="center"></div>

Backend code is in java.

Any help is appreciated!

Prerna shah
  • 321
  • 3
  • 20
  • PHP is server side, Client side JS is client side. You need to use Ajax but you are not. There is no ajax code in your question. Your second code is NOT Ajax. Move the php code to a php file and use $.ajax, $.get or $.post to access it on the server – mplungjan Sep 04 '18 at 06:04
  • The backend code is in java. Moreover my question here is why is the first code snippet not working without page refresh and second code is working perfectly! – Prerna shah Sep 04 '18 at 06:40
  • Because as with PHP, the code is running ONCE on the server and not again – mplungjan Sep 04 '18 at 06:47
  • oh ok ! yes understood. Sorry my bad :( – Prerna shah Sep 04 '18 at 09:15
  • Just replace PHP in my first comment with JSP – mplungjan Sep 04 '18 at 09:25
  • I changed the dupes to JSP. You can use DWR too: http://directwebremoting.org/dwr/index.html – mplungjan Sep 04 '18 at 09:28

0 Answers0