当前位置:主页 > 查看内容

通过Ajax进行Post提交Json数据的方法

发布时间:2021-04-18 00:00| 位朋友查看

简介:js代码 $.ajax({ type : "POST", url : js_path + "/maintainAdd/add", data : JSON.stringify(madd_data.editMaintain), contentType : "application/json", dataType : "json", complete:function(msg) { layer.msg("报修成功",{time:2000}); layer.close(……

js代码

$.ajax({ 
      type : "POST", 
      url : js_path + "/maintainAdd/add", 
      data : JSON.stringify(madd_data.editMaintain), 
      contentType : "application/json", 
      dataType : "json", 
      complete:function(msg) { 
        layer.msg("报修成功",{time:2000}); 
        layer.close(madd_data.w_c_index); 
      } 
    }); 

Action代码

<span style="white-space:pre;"> </span>@ResponseBody 
  @RequestMapping(value = "/add",method = RequestMethod.POST) 
  public void addMaintain(@RequestBody Maintain maintain){ 
    this.save_maintain(maintain); 
  } 

注意事项:

1、ajax中,contentType: "application/json"是必须的。dataType: "json"是表示返回值是json格式,依据返回值类型而定。

2、data中,将json对象序列化。使用JSON.stringfy()函数或者双引号形式的字符串。

3、调试的一个技巧,有时候json变量和实体类相对复杂时提交老是报415或者400的错误又找不到原因,可以将Action中的实体类换成JSONObject 看看能不能接收到参数,@RequestBody JSONObject requestJso,接收后在JSON系列化到实体类。有次就是因为json变量向实体类转换时发生字符向数字转换的错误。

以上这篇通过Ajax进行Post提交Json数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持尊托云数。


原文链接:https://m.jb51.net/article/137358.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐