简体   繁体   English

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

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

I am new to the jQuery and currently trying to implement an ajax call which will permanently poll the server and request some data. 我是jQuery的新手,目前正在尝试实现ajax调用,该调用将永久轮询服务器并请求一些数据。 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. ajax工作正常,因为我能够点击我的服务器端控制器方法但是在添加数据之后:gameLink param它停止工作。 Here is my jQuery function: 这是我的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>';
                });
            }
        }); 
    }

The $gameLink is present on the jsp as few lines below i am using it as $ gameLink出现在jsp上,我正在使用它作为下面几行

<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? 将$ gameLink添加为请求参数或我做错了什么的正确语法是什么?

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. 希望这可以帮助。

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

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

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

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