Node.js solves the problem of Chinese garbled characters in client request data Example code: var http = require('http'); var server = http.createServer(); server.on('request',function(req,res){ // res.end("hello world"); res.end("Hello World"); }); server.listen(3000,function(){ console.log("Server is running"); }); reason: The data sent by default on the server is actually UFT8 encoded content But the browser does not know that you are UFT8 encoded content If the browser does not know the encoding of the server's response content, it will execute it according to the default encoding of the current operating system. The default setting for Chinese operating systems is GBK Solution: The correct way is to tell the browser what type of data I am sending you var http = require('http'); var server = http.createServer(); server.on('request',function(req,res){ // res.end("hello world"); res.setHeader('Content-Type','text/plain;charset=utf-8'); res.end("Hello World"); }); server.listen(3000,function(){ console.log("Server is running"); });
Response content type Content-Typevar http = require('http'); var server = http.createServer(); server.on('request',function(req,res){ if(req.url==='/plain'){ res.setHeader('Content-Type','text/plain;charset=utf-8'); res.end("Hello World"); }else if(req.url==='/html'){ res.setHeader('Content-Type','text/html;charset=utf-8'); res.end("<h1>Hello World<br/> hello world</h1>"); } }); server.listen(3000,function(){ console.log("Server is running"); }); Return different types of Content-Type formats according to different request paths This is the end of this article about node.js's method to solve the problem of Chinese garbled characters in client request data. For more relevant node client request data garbled characters content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A detailed introduction to Linux system configuration (service control)
>>: Two ways to implement HTML to randomly drag content positions
The first step is to unzip the compressed package...
<br />Related articles: How to prompt and op...
1. Set the list symbol list-style-type: attribute...
Caused by: java.sql.SQLException: Incorrect strin...
1. dhtmlxTree dHTMLxTree is a feature-rich Tree M...
Table of contents 1. Aggregate Query 1. COUNT fun...
Table of contents Method 1: Routing meta informat...
In this article, we will need to learn how to vie...
The root account of mysql, I usually use localhos...
This article mainly introduces the differences be...
1. Update the entire table. If the value of a col...
The default ssh port number of Linux servers is g...
1. Install MySQL (1) Unzip the downloaded MySQL c...
In fact, XHTML 1.0 is divided into two types (thr...
The simple installation configuration of mysql5.7...