Home  >  Article  >  Web Front-end  >  What to do if jquery serialization submits Chinese garbled characters

What to do if jquery serialization submits Chinese garbled characters

藏色散人
藏色散人Original
2023-02-09 10:33:361585browse

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.

What to do if jquery serialization submits Chinese garbled characters

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(&#39;=&#39;);
obj[d[0]] = d[1];
}
if(obj.mobile == &#39;&#39;){
layer.msg("手机号不能为空");
return;
}
if(obj.plate == &#39;&#39;){
layer.msg("车牌号不能为空");
return;
}
if(obj.passWord == &#39;&#39; || obj.passWord2 == &#39;&#39;){
layer.msg("密码不能为空");
return;
}
if(obj.passWord != obj.passWord2){
layer.msg("两次密码不一致,请重新输入");
return;
}
my.post({
url:"user/register",
contentType : &#39;application/json;charset=utf-8&#39;, //设置请求头信息
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn