JS获取后台变量

JS获取后台变量,第1张

首先确定你后台传过来的str是你所说的那样

其次你js应该写成

var indexdata2 = <%=str %>;

去掉外边的双引号,因为你这个是js的数组,带双引号就是字符串了,所以你不能直接用

是当中的功能。原生js下拉刷新获取后端数据是当中的功能。原生是一款二次元3D开放世界冒险游戏,游戏发生在一个被称作“提瓦特”的幻想世界。在这里,被神选中的人将被授予“神之眼”,导引元素之力。玩家将扮演一位名为“旅行者”的神秘角色,在自由的旅行中邂逅性格各异。

在springmvc中controller的结果集可通过json格式传到js前端接受,也可以通过Map传给前端,具体实现如下

1,通过json格式传递

controller层实现如下

@RequestMapping("queryCityInfo")

@ResponseBody

public String queryCityInfo()throws Exception{

String provinceId = getString("id");

@SuppressWarnings("rawtypes")

List cityList = personalServicequeryCity(provinceId);

if(null != cityList && cityListsize() >0 ){

String json = JSONUtilstoJSONString(cityList);

superoutStr(json);

}

return null;

}

protected void outStr(String str)</span>

{

try

{

responsesetCharacterEncoding("UTF-8");

responsegetWriter()write(str);

}

catch (Exception e)

{

}

}

public static <T> String toJSONString(List<T> list)

{

JSONArray jsonArray = JSONArrayfromObject(list);

return jsonArraytoString();

}

js端接受如下

function selectBankCity(id){

$ajax({

url:baseAddress+"queryCityInfodoprovinceId="+id,

type:'get',

dataType:'json',

success:function(data){

$('#custBankArea')empty();

$('#custBankArea')append("<option >--请选择城市信息--</option>");

for(var i=0;i<datalength;i++){

$('#custBankArea')append("<option value='"+scjgcjcomdata[i]id+"'>"+data[i]cityName+"</option>");

}

}

});

}

2,通过Map传递

controller层实现如下

@RequestMapping("queryProvince")

@ResponseBody

public Map<String, Object>  queryProvince(>

有很多方法的,像你已经这样用setAttribute了,那么在客户端你就用getAttribute("cList")就能拿到cList了,不过你要用jsp的尖括号。另外最常用的是用struts标签,在action总对cList用一个set get方法,然后用<s:property value="cList"/>就可以了

个人意见,仅供参考

一般的做法是将map对象转成json在jsp页面中输出,js直接得到的就是json对象,便利json使用for循环即可

for(k in json){

alert(k + "=" + json[k])

}

获取id   ${userid}

获取name ${username}

获取company ${usercompany}

JS中直接从java后台获得对象的值(数组的值)

这里举得例子是:JS直接从后台Contorller中(SpringMVC中的model中)获得数值的值

Contorller 此处将 talentIntegralRecordsDay talentIntegralRecordsIntegral 两个数组用JSONtoJSONString()封装。

@SuppressWarnings("deprecation")

@RequestMapping("/integralParadise")    public ModelAndView Welcome(>

Users user = userServicecurrentUser(request);  

usergetTalentUser()getIntegral();

Systemoutprintln(usergetTime()getDate());

TalentIntegralRecord[] signInTalentIntegralRecords= wechatIntegralServicegetUserSignInTalentIntegralRecords(usergetId());        int size = signInTalentIntegralRecordslength;        int[] talentIntegralRecordsDay = new int[size];

Long[] talentIntegralRecordsIntegral = new Long[size];        for(int i=0;i<signInTalentIntegralRecordslength;i++){

talentIntegralRecordsDay[i]=signInTalentIntegralRecords[i]getOperatorTime()getDate();

talentIntegralRecordsIntegral[i]=signInTalentIntegralRecords[i]getIntegral();

}

Map<String,Object> map = new HashMap<String,Object>();

mapput("talentIntegralRecordsDay", JSONtoJSONString(talentIntegralRecordsDay));

mapput("talentIntegralRecordsIntegral", JSONtoJSONString(talentIntegralRecordsIntegral));        return new ModelAndView("wechat/integralParadise/rili",map);

}

前台JSP 因为用到两个数组数据的JS代码为页面引用的JS代码所以要在页面中先声明获得后台两个数组(这段JS代码应在引用的JS文件前面)

<script type="text/javascript">

$(document)ready(function(){

windowtalentIntegralRecordsDay = ${talentIntegralRecordsDay};

windowtalentIntegralRecordsIntegral = ${talentIntegralRecordsIntegral};

});</script>

引用的JS文件 开始就获得了两个数组的值

$(function() {    var signFun = function() {      

var dateArray = windowtalentIntegralRecordsDay;// 假设已经签到的

var talentIntegralRecordsIntegral = windowtalentIntegralRecordsIntegral;        var $dateBox = $("#js-qiandao-list"),

$currentDate = $("current-date"),

$qiandaoBnt = $("#js-just-qiandao"),

_html = '',

_handle = true,

myDate = new Date();

$currentDatetext(myDategetFullYear() + '年' + parseInt(myDategetMonth() + 1) + '月' + myDategetDate() + '日');        var monthFirst = new Date(myDategetFullYear(), parseInt(myDategetMonth()), 1)getDay();        var d = new Date(myDategetFullYear(), parseInt(myDategetMonth() + 1), 0);        var totalDay = dgetDate(); //获取当前月的天数

for (var i = 0; i < 42; i++) {

_html += ' <li><div class="qiandao-icon"></div></li>'

}

$dateBoxhtml(_html) //生成日历网格

var $dateLi = $dateBoxfind("li");        for (var i = 0; i < totalDay; i++) {

$dateLieq(i + monthFirst)addClass("date" + parseInt(i + 1));            for (var j = 0; j < dateArraylength; j++) {                if (i == dateArray[j]) {

$dateLieq(i)addClass("qiandao");                    var integral;                    if(talentIntegralRecordsIntegral[j]==0){

integral="大转盘";

}else{

integral="+"+talentIntegralRecordsIntegral[j];

}

$dateLieq(i)find("div")text(integral);

}

}

} //生成当月的日历且含已签到

$("date" + myDategetDate())addClass('able-qiandao');

$dateBoxon("click", "li", function() {                if ($(this)hasClass('able-qiandao') && _handle) {

$(this)addClass('qiandao');

qiandaoFun();

}

btn1: function(index){ //或者使用btn1

            //按钮按钮一的回调

            //layermsg('按钮按钮一的回调', {icon: 1});

            select = 1;

            select_return(index);

        },

        btn2: function(index){ //或者使用btn2

            //按钮按钮二的回调

            //layermsg('按钮按钮二的回调', {icon: 2});

            select = 2;

            select_return(index);

        },

        btn3: function(index){

            //按钮按钮三的回调

            //layermsg('按钮按钮三的回调', {icon: 3});

            select = 3;

            select_return(index);        

        }

        btn4: function(index){

            //按钮按钮4的回调

            //layermsg('按钮按钮4的回调', {icon: 4});

            select = 4;

            select_return(index);        

        },

        btn5: function(index){

            //按钮按钮5的回调

            //layermsg('按钮按钮5的回调', {icon: 5});

            select = 5;

            select_return(index);        

        }

把这一整段写成一个字符串返回给页面好了。想了半天没什么好方法,呵呵。主要是不知道你其他地方是怎么实现的。

以上就是关于JS获取后台变量全部的内容,包括:JS获取后台变量、原生js下拉刷新获取后端数据、js怎么获取从springmvc后台传过来的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10129012.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存