jQuery ajax call data param issue in spring mvc en

2019-08-05 08:33发布

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?

2条回答
我只想做你的唯一
2楼-- · 2019-08-05 09:03

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.

查看更多
神经病院院长
3楼-- · 2019-08-05 09:19

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}" }, 
查看更多
登录 后发表回答