小编典典

Spring MVC环境中的jQuery Ajax调用数据参数问题

spring-mvc

我是jQuery的新手,目前正在尝试实现ajax调用,该调用将永久轮询服务器并请求一些数据。Ajax运行正常,因为我可以使用服务器端控制器方法,但是添加
数据后:gameLink 参数已停止工作。这是我的jQuery函数:

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>';
                });
            }
        }); 
    }

$ gameLink出现在jsp上,因为我正在使用以下几行

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

将$ gameLink添加为请求参数的正确语法是什么,或者我做错了什么?


阅读 424

收藏
2020-06-01

共1个答案

小编典典

你有这样尝试过吗?

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>';
            });
        }
    }); 
}

要么

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

希望这可以帮助。

2020-06-01