Home  >  Article  >  Web Front-end  >  How to deal with Chinese garbled characters in jquery post

How to deal with Chinese garbled characters in jquery post

coldplay.xixi
coldplay.xixiOriginal
2020-11-25 14:16:072177browse

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.

How to deal with Chinese garbled characters in jquery post

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!

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