jquery get form data

一、serialize()方法
1、格式
var data = $("form").serialize();
2、功能
将表单内容序列化成一个字符串。
3、返回结果
username=forrest&passwd=1234&gender=0&interest=swimming&interest=running&interest=readBook
如果存在中文,需要转码
Reasons and solutions for Chinese garbled characters after jquery form.serialize() serialization
Reason: .serialize() automatically calls the encodeURIComponent method to encode the data 
Solution: call decodeURIComponent(XXX,true); decode the data 
E.g: 
var params = jQuery("#formId").serialize(); // http request parameters. 
params = decodeURIComponent(params,true);
coding
params = encodeURI(encodeURI(params));
  Backstage
String paramsTrans = new String(params.getBytes("ISO-8859-1"),"UTF-8");
params = java.net.URLDecoder.decode(paramsTrans , "UTF-8");
problem solved.
----------------------------------------------------------------------------------------------------------------------------------
Note: The data sent by the page is encoded with URI twice . The advantage of this is that no matter the browser user sets the encoding on the page, the encoding used by the server is converted into UTF-8 by URL encoding once.
The encodeURL function is mainly used to transcode the URI, and it uses the UTF-8 encoding by default.
  Specifically specify 2 encodeURIs:
The specific principle analysis is as follows. Assuming that the Chinese input on the page side is a "中", follow the steps below to decode
1.第一次encodeURI, 按照utf-8方式获取字节数组变成[-28,-72-83],对字节码数组进行遍历,把每个字节转化
成对应的16进制数,这样就变成了[E4,B8,AD],最后变成[%E4,%B8,%AD]
2.第二次encodeURI,把数组最后变成 [%25E4,%25B8,%25AD] 然后就把处理后的数据 [%25E4,%25B8,%25AD]
发往服务器端,当应用服务器调用getParameter方法,getParameter方法会去向应用服务器请求参数
应用服务器最初获得的就是发送来的 [%25E4,%25B8,%25AD] ,应用服务器会对这个数据进行URLdecode操作,
URldecode操作和encodeURL操作是相反的操作,处理结果就是 [%E4,%B8,%AD] ,并把这个值返回
给getParameter方法
然后再在服务器端中调用相应的URL转码方法或者是函数  就可以把数据还原成最初页面发送过来的中文 “中” 了。
 
二、serializeArray()方法
1、格式
var jsonData = $("form").serializeArray();

2、功能
将页面表单序列化成一个JSON结构的对象。注意不是JSON字符串。
 
3、返回结果:
[{"name":"lihui", "age":"20"},{...}] 获取数据为 jsonData[0].name。


jquery扩展函数:


$.fn.serializeObject = function(para)    
{    
var serializeObj={}; // 目标对象 
var tempObj={};//临时对象
    var array=this.serializeArray(); // 转换数组格式
    if(para!=null&&para!=undefined){
    $.each(para,function(name,value) {
    array.push({"name":name,"value":value});
    });
    }
    console.log(para);
    console.log(array);
    $(array).each(function(){ // 遍历数组的每个元素 {name : xx , value : xxx} 
        if(serializeObj[this.name]){ // 判断对象中是否已经存在 name,如果存在name 
              if($.isArray(serializeObj[this.name])){ 
                 serializeObj[this.name].push(this.value); // 追加一个值 hobby : ['音乐','体育'] 
              }else{ 
                      // 将元素变为 数组 ,hobby : ['音乐','体育'] 
                 serializeObj[this.name]=[serializeObj[this.name],this.value]; 
              } 
        }else{ 
            serializeObj[this.name]=this.value; // 如果元素name不存在,添加一个属性 name:value 
        } 
    });     
   return serializeObj;    
};


附:
字符串转对象(strJSON代表json字符串)
   var obj = eval(strJSON);
   var obj = strJSON.parseJSON();
   var obj = JSON.parse(strJSON);
json对象转字符串(obj代表json对象)
   var str = obj.toJSONString();
   var str = JSON.stringify(obj)
运用时候需要除了eval()以外需要json.js包(切记哦)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326809464&siteId=291194637