1

I am new to the jQuery and currently trying to implement an ajax call which will permanently poll the server and request some data. The ajax is working fine as i was able to hit my server side controller method however after adding a data: gameLink param it is stopped working. Here is my jQuery function:

window.setInterval(pollActiveParticipants, 10000);
    function pollActiveParticipants() { 
        $.ajax({
            type: "GET",
            url: "pollActiveParticipants",
            data: {"gameLink": $gameLink },    //this is where i need help! 
            dataType: 'json',
            success: function(data){
                $.each(data, function(index, value) {
                    '<p>' + value.username + '</p><br>';
                });
            }
        }); 
    }

The $gameLink is present on the jsp as few lines below i am using it as

<br>
 Other participants can access the game on the following url: &nbsp; ${gameLink} 
<br>

What is the correct syntax to add the $gameLink as request param or what I am doing wrongly?

1
  • you are welcome. It avoids complexity. Commented Jan 16, 2014 at 8:40

2 Answers 2

2

Have you tried like this?

function pollActiveParticipants() { 
 var gameLink = '${gameLink}';

 //Make sure it is having the value here.
 //alert(gameLink); or console.log(gameLink);

    $.ajax({
        type: "GET",
        url: "pollActiveParticipants",
        data: {"gameLink": gameLink },   
        dataType: 'json',
        success: function(data){
            $.each(data, function(index, value) {
                '<p>' + value.username + '</p><br>';
            });
        }
    }); 
} 

or

var gameLink = '${gameLink}';    //previously '<%=gameLink %>', not recommended 
url: "pollActiveParticipants?gameLink="+gameLink,
dataType: 'json', 
...

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Works fine. Why it needs to extract to a separate variable and using of the variable inside of the jquery? But yeah thanks for the answer.
It doesn't - did you try my answer below (posted first BTW)?
@nickdos - The part var gameLink = '${gameLink}';data: {"gameLink": gameLink }, is working fine for me and if I alert the gamelink it writes me the correct output. But yes as soon as i'll get a chance, i'll try what you suggested as it is quite elegant solution also. I appreciate both of you answers guys!
1

I'll take a stab and guess $gameLink is a GSP var and not a JS var... In which case you need to string quote it thus:

data: {"gameLink": "${gameLink}" }, 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.