最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 22:51:34
文档

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

通过Ajax进行Post提交Json数据的方法:js代码 $.ajax({ type : POST, url : js_path + /maintainAdd/add, data : JSON.stringify(madd_data.editMaintain), contentType : application/json, dataType : json, complete:func
推荐度:
导读通过Ajax进行Post提交Json数据的方法:js代码 $.ajax({ type : POST, url : js_path + /maintainAdd/add, data : JSON.stringify(madd_data.editMaintain), contentType : application/json, dataType : json, complete:func


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

文档

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

通过Ajax进行Post提交Json数据的方法:js代码 $.ajax({ type : POST, url : js_path + /maintainAdd/add, data : JSON.stringify(madd_data.editMaintain), contentType : application/json, dataType : json, complete:func
推荐度:
标签: 数据 json ajax
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top