JFinal使用笔记5

1、前台代码

1$.ajax({ 2 type : "POST", 3 url : "/activity", 4 dataType : "json", 5 success : function(data) { 6 alert("success"); 7 console.info(data); 8 }, 9 error : function(textStatus) { 10 alert("error"); 11 console.info(msg); 12 } 13});

2、后台代码

1public void index() { 2    setAttr("activityList", Activity.dao.find("select * from activity order by id asc")); 3    renderJson(); 4}

折腾了一晚上终于可以用ajax访问JFinal,并成功接收到返回值。
还是看了@JFinal对一个问题文章的回复才搞定的。

@JFinal 说渲染 json 数据更简单,下面给出三个例子:

1List<User> userList = User.dao.find("select * from user"); 2render("userList", userList);

以上代码会将 userList 这个对象全部转化为 json 并发送给 client。

1setAttr("user", User.dao.findById(getParaToInt())); 2setAttr("blogList", Blog.dao.find("select * from blog where user_id=?", getParaToInt()); 3renderJson();

以上代码会将 user 与 blogList对象转化为 json 并发送给client。

您还可以这样来:

renderHtml(JsonKit.toJson(blogList));

我在这个过程中遇到的问题包括:
1、试图对一个json格式的字符串使用renderJson,结果前台调用了error的function,并且在页面上打印了这个字符串。
2、原来写的ajax的url,修改后点击原来触发事件的button,新的url不生效,如果新建一个button去触发就可以生效,不知道是什么情况。

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

springboot2之优雅处理返回值

前言最近项目组有个老项目要进行前后端分离改造,应前端同学的要求,其后端提供的返回值格式需形如{"status":0,"message":"success","data":{}}方便前端数据处理。要实现前端同学这个需求,其实也挺简单的,

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

两行Python代码将JSON文本转换成标准字典

在获取网页报文时,经常遇到JSON格式的文本。如:\\\html"code":200,"message":"success","data":   "count":47,"pageCount":3,"floorCount":27,"list":     \       "info":           "

SpringMVC中使用JSON

前台发送:传递JSON对象functionrequestJson(){$.ajax.({type:“post”,url:“${pageContext.request.contextPath}/testJson/responseJson”,