繁体   English   中英

jQuery ajax在spring mvc环境中调用数据参数问题

[英]jQuery ajax call data param issue in spring mvc environment

我是jQuery的新手,目前正在尝试实现ajax调用,该调用将永久轮询服务器并请求一些数据。 ajax工作正常,因为我能够点击我的服务器端控制器方法但是在添加数据之后:gameLink param它停止工作。 这是我的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添加为请求参数或我做错了什么的正确语法是什么?

你试过这样的吗?

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', 
...

希望这可以帮助。

我会采取$gameLink并猜测$gameLink是一个GSP var而不是JS var ...在这种情况下你需要字符串引用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM