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
1. Download mysql-5.7.17-winx64.zip; Link: https:...
Find the problem Today, when I tried to modify th...
When Mysql occupies too much CPU, where should we...
Icon icon processing solution The goal of this re...
In fact, this is very simple. We add an a tag to ...
question The seamless scrolling of pictures and t...
1. SSH remote management SSH Definition SSH (Secu...
(?i) means do not match case. Replace all uppercas...
Table of contents Mind Map Simple understanding E...
The Golden Rule No matter how many people are wor...
Zero: Uninstall old version Older versions of Doc...
1. Browser rendering mode and doctype Some web pa...
1. Environmental preparation: Operating system: C...
Docker has many log plug-ins. The default is to u...
ylbtech_html_print HTML print code, support page t...