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

How to deal with Chinese garbled characters in jquery post

coldplay.xixi
Release: 2020-11-30 14:23:53
Original
2230 people have browsed it

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("保存失败!");        
  }
}
);
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template