Home Web Front-end JS Tutorial Javascript and Ajax Chinese garbled vomiting blood version solution_javascript skills

Javascript and Ajax Chinese garbled vomiting blood version solution_javascript skills

May 16, 2016 pm 06:38 PM
ajax javascript Chinese garbled

Today I have been dealing with the Ajax Chinese garbled code problem. There are two types of Ajax garbled code problems:

1. Chinese garbled code output by JavaScript,

For example: alert("Chinese garbled code test");

The solution is relatively simple, which is to set all charset and pageEncoding values ​​in jsp to the same, usually utf-8.

2. The second problem is that the data obtained by Ajax from the server is garbled. (I searched for n hours and tried n methods to find the answer)

Now I will share with you the more effective methods I have collected: (The development environment I use is Eclipse, I believe other languages ​​and development environments are similar.)

For example

var message = xmlHttp.responseText;
alert("message: " message);

The message output is garbled

Solution:

1. Modify the encoding. Remember to back up the code. After changing the encoding, the Chinese will become garbled.

Right-click Properties on the js file, Modify Text file encode to UTF-8 ( This should be the same as the encoding in jsp)

Similarly set the Default encoding of JavaScript source file and JSP to UTF-8 (This should be the same as the encoding in jsp, In this way, future projects will be It is utf-8 encoding, it is recommended to use this)

2. Pay attention to the positional relationship between response.setContentType("text/html;charset=utf-8"); and PrintWriter out = response.getWriter();. Remember to replace PrintWriter out = response.getWriter(); is placed after response.setContentType("text/html;charset=utf-8");, otherwise the set encoding will be invalid, which is why I couldn't figure it out after trying it for a long time! ! !

Problem solved.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Methods to solve the problem of Chinese garbled characters in PHP Dompdf Methods to solve the problem of Chinese garbled characters in PHP Dompdf Mar 05, 2024 pm 03:45 PM

Methods to solve the Chinese garbled problem of PHPDompdf PHPDompdf is a tool for converting HTML documents to PDF files. It is powerful and easy to use. However, when processing Chinese content, you sometimes encounter the problem of garbled Chinese characters. This article will introduce some methods to solve the Chinese garbled problem of PHPDompdf and provide specific code examples. 1. When using font files to process Chinese content, a common problem is that Dompdf does not support Chinese content by default.

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

The ultimate solution to the problem of Chinese garbled characters in PyCharm The ultimate solution to the problem of Chinese garbled characters in PyCharm Jan 27, 2024 am 08:00 AM

The ultimate method to solve the problem of Chinese garbled characters in PyCharm requires specific code examples. Introduction: PyCharm, as a commonly used Python integrated development environment (IDE), has powerful functions and a friendly user interface, and is loved and used by the majority of developers. However, when PyCharm processes Chinese characters, it may sometimes encounter garbled characters, which causes certain problems in development and debugging. This article will introduce how to solve the Chinese garbled problem in PyCharm and give specific code examples. 1. Set up the project

Common causes and solutions for Chinese garbled characters in MySQL installation Common causes and solutions for Chinese garbled characters in MySQL installation Mar 02, 2024 am 09:00 AM

Common reasons and solutions for Chinese garbled characters in MySQL installation MySQL is a commonly used relational database management system, but you may encounter the problem of Chinese garbled characters during use, which brings trouble to developers and system administrators. The problem of Chinese garbled characters is mainly caused by incorrect character set settings, inconsistent character sets between the database server and the client, etc. This article will introduce in detail the common causes and solutions of Chinese garbled characters in MySQL installation to help everyone better solve this problem. 1. Common reasons: character set setting

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

What should I do if the PHP web page has Chinese garbled characters? A complete solution What should I do if the PHP web page has Chinese garbled characters? A complete solution Mar 26, 2024 pm 03:27 PM

The problem of Chinese garbled characters in PHP web pages is that Chinese characters are displayed as garbled characters in the web page display. This situation is usually caused by inconsistent encoding or the character set is not set. Solving the problem of Chinese garbled characters in PHP web pages requires starting from many aspects. The following are some common solutions and specific code examples. Set the PHP file encoding: First make sure that the encoding of the PHP file itself is UTF-8. You can set the UTF-8 encoding when saving in the editor, or add the following code to the header of the PHP file to set the encoding: &l

Methods to solve the problem of Chinese garbled characters in Linux system Methods to solve the problem of Chinese garbled characters in Linux system Feb 19, 2024 am 09:22 AM

The problem of Linux Chinese garbled characters is a problem that many Chinese users often encounter when using Linux systems. The main reason for garbled Chinese characters is that the default character encoding used by the Linux system is UTF-8, but some software or applications may not be compatible with UTF-8 encoding, causing Chinese characters to not be displayed correctly. There are many ways to solve this problem. Several common solutions will be detailed below and specific code examples will be provided. Modify the terminal character encoding settings: The terminal character encoding settings determine whether the terminal can correctly

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

See all articles