Jquery post Chinese garbled solution: 1. Encode the sent data with [encodeURIComponent()] when making a front-end post request; 2. Translate it with [UTF-8] in the background.
The operating environment of this tutorial: Windows 7 system, jquery version 3.2.1. This method is suitable for all brands of computers.
Solution to Chinese garbled jquery post:
Encode the data sent when making a front-end post requestencodeURIComponent()
encoding
For example:
var transactType= $("#transactType").attr("value"); var content=encodeURIComponent($("#content").html()); var title=encodeURIComponent($("#title").val()); $.post( "${path}/transact!addTransact.action", {"content":content,"title":title}, function(data){ if(data=='1'){ alert("保存成功!"); DG.cancel(); }else{ alert("保存失败!"); } } );
Backend:
Use UTF-8
to translate
transactType = URLDecoder.decode(getStringParameter("transactType"),"UTF-8"); content = URLDecoder.decode(getStringParameter("content"),"UTF-8"); title = URLDecoder.decode(getStringParameter("title"),"UTF-8");
to solve the jQuery post request Chinese garbled problem.
Related learning recommendations: js video tutorial
The above is the detailed content of How to deal with Chinese garbled characters in jquery post. For more information, please follow other related articles on the PHP Chinese website!