Home  >  Article  >  Web Front-end  >  What should I do if jquery’s ajax() function passes Chinese garbled values?

What should I do if jquery’s ajax() function passes Chinese garbled values?

coldplay.xixi
coldplay.xixiOriginal
2020-11-25 14:57:141772browse

The solution to the Chinese garbled value passed by jquery's ajax() function: 1. Use encodeURI to encode twice before submitting; 2. Ajax configures the contentType attribute and adds [charset=UTF-8].

What should I do if jquery’s ajax() function passes Chinese garbled values?

The operating environment of this tutorial: Windows 7 system, jquery version 3.2.1. This method is suitable for all brands of computers.

The solution to the Chinese garbled value passed by jquery's ajax() function:

Method 1: Use encodeURI to encode twice before submitting, remember it must be Twice

1. Modify the following code

The code is as follows:

data:{id:1, type:encodeURI(encodeURI(‘商品'))}

2. Decode the obtained string in the background action

(1), String type = request.getParameter(“type”);

(2), type = URLDecoder.decode(type, “UTF-8″ );

Method 2: ajax configure the contentType attribute, add charset=UTF-8

Add the following parameters to the ajax method

contentType: "application/x-www-form-urlencoded; charset=UTF-8" It's similar when using other js frameworks or xhr. Just set the contentType in the header.

The key here is charset =UTF-8, if you don’t have this, it won’t work. By default, there is no contentType in jQuery.

Related free learning recommendations: javascript(Video)

The above is the detailed content of What should I do if jquery’s ajax() function passes Chinese garbled values?. 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