jQuery向后台传入json格式数据的方法

来源:互联网 发布:2016中国象棋软件排名 编辑:程序博客网 时间:2024/05/22 14:18

jQuery向后台传入json格式

本文实例讲述了jQuery向后台传入json格式数据的方法。分享给大家供大家参考。具体分析如下:

前后台数据交互一般都用json格式,后台可以直接将json对应转化为实体对象。方便以后的操作。jQuery向后台传数据的时候,我们会发现他会自动转化成查询字符串,不能真正传入一个json。而且用jquery对表单序列化的时候,返回的格式是一个数组,还需要作进一步转换。其实只要我们在ajax方法中配置一些东西就可以完成。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<form id="ff">
  <input type="text"name="test1"/>
  <input type="text"name="test2"/>
  <input type="text"name="test3"/>
  <input type="text"name="test4"/>
  <input type="button"id="save" value="save"/>
</form>
 
$("#save").on("click",function () {
 varparams = $("#ff").serializeArray();
 varj = {};
 for(var item in params) {
   j[params[item].name] = params[item].value;
 }
 
 $.ajax({
   url:'index.html',
   data:JSON.stringify(j),
   type:'post',
   dataType:'json',
   headers:{
 Accept:"application/json",
 "Content-Type":"application/json"
   },
   processData:false,
   cache:false
 }).done(function(data) {
 });
 
});
数据的方法

阅读全文
0 0