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!

6
  • 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 Commented Sep 4, 2018 at 6: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! Commented Sep 4, 2018 at 6:40
  • Because as with PHP, the code is running ONCE on the server and not again Commented Sep 4, 2018 at 6:47
  • oh ok ! yes understood. Sorry my bad :( Commented Sep 4, 2018 at 9:15
  • Just replace PHP in my first comment with JSP Commented Sep 4, 2018 at 9:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.