Home  >  Article  >  Web Front-end  >  nodejs post Chinese garbled code

nodejs post Chinese garbled code

WBOY
WBOYOriginal
2023-05-11 12:23:36522browse

Node.js is a popular server-side JavaScript engine that is widely used in web development. However, when processing POST requests, Chinese characters often appear garbled, which may cause a series of problems.

The reason for the problem is that in the HTTP protocol, the data entity part of the POST request is encoded through the encoding method specified by the Content-Type header. If the client sending the request and the server receiving the request use different encoding methods, Chinese characters will be garbled.

So, the key to solving this problem is to ensure that the same encoding is used when requesting data and receiving data. The following are some solutions:

  1. Specify the request encoding method

When sending a POST request, you can specify the encoding method of the request data by setting the Content-Type header. If the request contains Chinese characters, you can set the Content-Type header to application/x-www-form-urlencoded;charset=utf-8, that is, use UTF-8 encoding for encoding:

const data = new URLSearchParams();
data.append('username', '张三');
data.append('password', '123456');
fetch('/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  },
  body: data
});
  1. Specify the response encoding method

When receiving a POST request, you can specify the encoding method of the response data by setting the Content-Type header. Similarly, if the response contains Chinese characters, you can set the Content-Type header to text/html;charset=utf-8, that is, use UTF-8 encoding for encoding:

app.post('/login', (req, res) => {
  res.setHeader('Content-Type', 'text/html;charset=utf-8');
  res.send('<p>登录成功</p>');
});
  1. Use the middle Middleware processing encoding

You can use middleware to handle the encoding problem of POST requests. The body-parser middleware is a common solution that automatically handles the encoding of request data, ensuring that the same encoding is used when receiving and sending data in the request:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/login', (req, res) => {
  const { username, password } = req.body;
  console.log(username, password);
  res.send('<p>登录成功</p>');
});

In this example, body - The parser middleware is added to the application and used to handle the encoding of POST requests.

In general, to deal with the Chinese garbled problem in POST requests, you need to ensure that the encoding of the request and response is consistent, and use middleware correctly to handle the encoding problem. These solutions can ensure that the application does not have Chinese garbled problems when processing POST requests, thereby better handling the communication between the user and the server.

The above is the detailed content of nodejs post Chinese garbled code. 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