Spring MVC环境中的jQuery Ajax调用数据参数问题 [英] jQuery ajax call data param issue in spring mvc environment

查看:77
本文介绍了Spring MVC环境中的jQuery Ajax调用数据参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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>

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

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

推荐答案

您是否尝试过这样?

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

希望这会有所帮助.

这篇关于Spring MVC环境中的jQuery Ajax调用数据参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆