首先看一下我在控制器Controller中的代码 public JsonResult SearchUnitByID(string ID) { Flow_StateUnitDefine 如果前端请求用的是get,那么控制器中要有相应的调整,代码如下: public JsonResult SearchUnitByID(string ID) {
本篇文章着重介绍在进行Ajax调用中经常使用的两个ActionResult,即JavaScriptResult和JsonResult。[本文已经同步到《How ASP.NET MVC Works?》 三、JsonResult JavaScript已经在Web应用中得到广泛的应用,而JSON则成了标准的数据格式。 如下面的代码片断所示,JsonResult具有一个object类型的属性Data表示需要被转换成JSON格式的数据对象。 我们可以通过它的JsonRequestBehavior属性开启JsonResult对HTTP-GET请求的支持。 JsonResult的JsonRequestBehavior属性在初始化的时候被设置为DenyGet,如果我们需要用创建的JsonResult来响应HTTP-GET请求,需要显式地将它的JsonRequestBehavior
Data { get; set; } public JsonResult(){} public JsonResult(bool success) { Success Success() { return new JsonResult(true); } public static JsonResult Success(Object data) { return new JsonResult(true, data); } public static JsonResult Fail() { return new JsonResult(false); } public static JsonResult Fail(ResultCode resultCode static JsonResult Fail(string msg) { return new JsonResult(false, msg); } public
} public static JSONResult ok(Object data) { return new JSONResult(data); } public static JSONResult ok() { return new JSONResult(null); } public static JSONResult errorMsg (String msg) { return new JSONResult(500, msg, null); } public static JSONResult errorMap (Object data) { return new JSONResult(501, "error", data); } public static JSONResult JSONResult errorException(String msg) { return new JSONResult(555, msg, null); } public
[AjaxOnly] [HttpGet] public virtual JsonResult ListData() { ..... ,代码如下: using PageAdmin.Utils; namespace System.Web.Mvc { //采用Newtonsoft.Json定义新的Jsonresult,默认的JsonResult public class NewtonJson:JsonResult { public override void ExecuteResult(ControllerContext context) = null) context.HttpContext.Response.Write(JsonHelper.JsonParse(Data)); } internal static JsonResult , ContentType = "application/json;charset=utf-8", }; return jsonResult; } } } 然后再控制器中重写Json方法。
> addUser (@RequestBody User user){ JsonResult result = new JsonResult(); try { users.put > getUserList (){ JsonResult result = new JsonResult(); try { List<User> userList = new > getUserById (@PathVariable(value = "id") Integer id){ JsonResult result = new JsonResult(); > updateById (@PathVariable("id") Integer id, @RequestBody User user){ JsonResult result = new JsonResult > deleteById (@PathVariable(value = "id") Integer id){ JsonResult result = new JsonResult();
例如,可以设计为: @ExceptionHandler public JsonResult<Void> aaaaa(Throwable e) { JsonResult<Void> jsonResult = new JsonResult<>(); if (e instanceof UsernameDuplicateException) { jsonResult.setState ; } else { jsonResult.setState(998); jsonResult.setMessage("操作失败! <Void> handleException(Throwable e) { JsonResult<Void> jsonResult = new JsonResult<>(); if (e ; } else { jsonResult.setState(998); jsonResult.setMessage("操作失败!出现了不可识别的问题,请联系系统管理员!")
> getUserById (@PathVariable(value = "id") Integer id){ JsonResult r = new JsonResult(); try { > getUserList (){ JsonResult r = new JsonResult(); try { List<User> userList = new ArrayList<User > add (@RequestBody User user){ JsonResult r = new JsonResult(); try { users.put(user.getId(), > update (@PathVariable("id") Integer id, @RequestBody User user){ JsonResult r = new JsonResult(); ; } } Json格式输出类 JsonResult.class package cn.saytime.bean; public class JsonResult { private String
} public static JSONResult ok(Object data) { return new JSONResult(data); } public static JSONResult ok() { return new JSONResult(null); } public static JSONResult JSONResult errorTokenMsg(String msg) { return new JSONResult(502, msg, null); } public static JSONResult errorException(String msg) { return new JSONResult(555, msg, null); } public JSONResult() { } public JSONResult(Integer status, String msg, Object
前后端分离方案 重点 枚举类构造方法,统一错误代码 统一JSON返回格式,JsonResult(大袋子) 设置返回的格式为json # 自定义登录成功返回 Spring Security自带一个login Controller package com.zr.handler; import com.alibaba.fastjson.JSON; import com.zr.common.JsonResult (ResultCode.USER_NOT_LOGIN))); } } # 自定义权限不足处理方案 继承 JsonResult.fail()可以直接接收枚举 @Component public list(@RequestBody SysUser sysUser){ return JsonResult.success("查询成功"); } @PostMapping("/add") @ JsonResult.success("新增成功"); } # 9.
<User> getUserById(@PathVariable("id")Long id){ JsonResult<User> result=new JsonResult<User>(); <User> insertSelective(User user){ JsonResult<User> result=new JsonResult<User>(); try { <Object> deleteByPrimaryKey(Long id){ JsonResult<Object> result=new JsonResult<Object>(); try <Object> getUserPages(Integer pageNum,Integer pageSize){ JsonResult<Object> result=new JsonResult () { this.isTrue=true; this.code ="0000"; } public JsonResult(Throwable throwable
post请求返回结果 CloseableHttpClient httpClient = HttpClients.createDefault(); JSONObject jsonResult " + url, e); } finally { httpPost.releaseConnection(); } return jsonResult post请求返回结果 CloseableHttpClient httpClient = HttpClients.createDefault(); JSONObject jsonResult " + url, e); } finally { httpPost.releaseConnection(); } return jsonResult */ public static JSONObject httpGet(String url) { // get请求返回结果 JSONObject jsonResult
jsonResult = new JsonResult(); jsonResult.setSuccess(true); jsonResult.setMsg(now.toLocaleString getTime() { Date now = new Date(); JsonResult jsonResult = new JsonResult(); jsonResult.setSuccess(true); jsonResult.setMsg(now.toLocaleString()); return jsonResult checkUsername(String username){ // 数据库查询, 模拟一下 lony JsonResult jsonResult = new JsonResult jsonResult.setSuccess(true); jsonResult.setMsg("登录成功"); }else { jsonResult.setSuccess
} public JsonResult<? 自定义限流处理类: public class CustomerBlockHandler { public static JsonResult<? public static JsonResult<? > fallback(@PathVariable("id") Long id){ JsonResult<? > fallback(@PathVariable("id") Long id){ JsonResult<?
if (xhr.status==200) { var resStr = xhr.responseText; jsonResult = eval("(" + resStr + ")"); AddGradesToSelectControl(jsonResult.grades); AddClassesToSelectControl(jsonResult.grades[0].id); document.getElementById("msgDiv ; i++) { if (gradeId == jsonResult.classes[i].cgId) { var option = new Option(jsonResult.classes[i].name, jsonResult.classes[i].id); selClasses.options.add
pojo.Users; import com.idig8.pojo.vo.UsersVO; import com.idig8.service.UserService; import com.idig8.utils.JSONResult userService; @ApiOperation(value="用户注册",notes="用户注册的接口") @PostMapping("/regist") public JSONResult ; } UsersVO userVO = setUserRedisSessionToken(user); return JSONResult.ok(userVO ); } @ApiOperation(value="用户登录",notes="用户登录的接口") @PostMapping("/login") public JSONResult ; } UsersVO userVO = setUserRedisSessionToken(userObject); return JSONResult.ok
validate(@Valid @RequestBody ValidationDemo demo, BindingResult bindingResult){ JSONResult jsonResult jsonResult.setData("校验失败"); jsonResult.setMsg(e.getDefaultMessage()); }); jsonResult.setData("校验成功"); jsonResult.setMsg(""); } return jsonResult; jsonResult.setData("校验失败"); jsonResult.setMsg(e.getDefaultMessage()); }); jsonResult.setData("校验成功"); jsonResult.setMsg(""); } return jsonResult; }
@ResponseBody @PostMapping(value = "/review") @Authentication(role = {2}) public JsonResult jsonResult = null; if (ListEnum.ZERO.toString().equals(aduitDTO.getType())) { if = JsonResult.buildSuccessResult("审核通过"); } if (null ! yyyy-MM-dd HH:mm:ss")); status = auditService.updateAduit(aduitDTO); jsonResult = JsonResult.buildSuccessResult("审核不通过"); } } return jsonResult; }
jsonResult=new JsonResult(); //要删除的id int deleteId=-1; jsonResult.setCode(0) <Integer> jsonResult = new JsonResult<>(); if (file == null) { jsonResult.setCode ; jsonResult.setSuccess(false); jsonResult.setData(0); return jsonResult i)(xlsx)$")) { jsonResult.setCode(202); jsonResult.setMsg("导入文件格式不正确! jsonResult.setMsg("成功导入模板!!")
/