JFinal使用笔记5

Stella981
• 阅读 522

1、前台代码

$.ajax({
    type : "POST",
    url : "/activity",
    dataType : "json",
    success : function(data) {
        alert("success");
        console.info(data);
    },
    error : function(textStatus) {
        alert("error");
        console.info(msg);
    }
});

2、后台代码

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

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

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

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

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

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

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

您还可以这样来:

renderHtml(JsonKit.toJson(blogList));

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

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Karen110 Karen110
2年前
两行Python代码将JSON文本转换成标准字典
在获取网页报文时,经常遇到JSON格式的文本。如:\\\html"code":200,"message":"success","data":   "count":47,"pageCount":3,"floorCount":27,"list":     \       "info":           "
Easter79 Easter79
2年前
springboot2之优雅处理返回值
前言最近项目组有个老项目要进行前后端分离改造,应前端同学的要求,其后端提供的返回值格式需形如{"status":0,"message":"success","data":{}}方便前端数据处理。要实现前端同学这个需求,其实也挺简单的,
Stella981 Stella981
2年前
Ajax各个参数的说明
Ajax各个参数的说明$.ajax({     url: "http://www.hzhuti.com",    //请求的url地址     dataType: "json",   //返回格式为json     async: true, //请求是否异步,默认为异步,这也是ajax重要特性   
Stella981 Stella981
2年前
Ajax和SpringMVC之间JSON交互
Ajax和SpringMVC之间的json数据传输有两种方式:1.直接传输Json对象2.将Json序列化成json字符串1.直接传输Json对象前端Ajax$(document).ready(function(){$("btn_login").click(function(){
Easter79 Easter79
2年前
SpringMVC中使用JSON
前台发送:传递JSON对象functionrequestJson(){$.ajax.({type:“post”,url:“${pageContext.request.contextPath}/testJson/responseJson”,
Stella981 Stella981
2年前
Ajax跨域请求COOKIE无法带上的解决办法
$.ajax({type:"get",dataType:"json",url:"http://sso.test.com/userinfo",crossDomain:true,xhrFields:{withCredentials:true
Stella981 Stella981
2年前
Es6获取数据
$(function(){$.ajax({//请求方式type:"GET",//文件位置url:"js/data.json",//返回数据格式为json,也可以是其他格式如dataType:"json",//请求成功后要执行的函数,拼接htmlsuccess:function(res){var
Stella981 Stella981
2年前
JS弹窗确认&Ajax封装方法
代码示例:varmodification{method:function(){//开始发送数据$.post({//请求登录处理页url:"/stock/modification",dataType:"json",//传送请求数据data:{