Home > Article > Web Front-end > What to do if jquery serialization submits Chinese garbled characters
Solution to jquery serialization submission of Chinese garbled characters: 1. Open the corresponding jquery file; 2. Decode the data by calling the "decodeURIComponent(XXX,true);" method to solve the garbled code problem.
The operating environment of this tutorial: Windows 10 system, jquery version 3.2.1, DELL G3 computer
How to submit Chinese garbled characters through jquery serialization manage?
When using serialize() in jquery to serialize the form, the problem of Chinese garbled characters
//登录 $(".register-btn").click(function() { var form = $("#register-form").serialize(); //序列化中文时之所以乱码是因为.serialize()调用了encodeURLComponent方法将数据编码了 //原因:.serialize()自动调用了encodeURIComponent方法将数据编码了 //解决方法:调用decodeURIComponent(XXX,true);将数据解码 form = decodeURIComponent(form,true); //关键点 var formArray = form.split("&"); var obj = {}; for(var i = 0;i<formArray.length;i++){ var d = formArray[i].split('='); obj[d[0]] = d[1]; } if(obj.mobile == ''){ layer.msg("手机号不能为空"); return; } if(obj.plate == ''){ layer.msg("车牌号不能为空"); return; } if(obj.passWord == '' || obj.passWord2 == ''){ layer.msg("密码不能为空"); return; } if(obj.passWord != obj.passWord2){ layer.msg("两次密码不一致,请重新输入"); return; } my.post({ url:"user/register", contentType : 'application/json;charset=utf-8', //设置请求头信息 data:JSON.stringify(obj), done:function(result){ if(result.code == 200){ setTimeout(function(){ location.href = "./home"; },2000); } } }); });
Recommended study: "jQuery Video Tutorial"
The above is the detailed content of What to do if jquery serialization submits Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!