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数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

点赞(193)

评论列表共有 0 条评论

立即
投稿
返回
顶部