Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the Chinese garbled event in the client request data by node.js

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how node.js solves the incident of Chinese garbled codes in the client request data. The editor finds it very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Node.js solves the event of Chinese garbled in the data requested by the client.

For 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 doesn't know that you are UFT8-encoded content.

The browser will execute according to the default encoding of the current operating system without knowing the encoding of the server's response content.

The Chinese operating system defaults to GBK.

Solution: the correct way is to tell the browser what kind of data res.setHeader ('Content-Type','text/plain;charset=utf-8') I'm sending you; content type, be careful not to miswrite the connectors, and don't scribble utf-8. In the http protocol, content-Type is used to tell the other party what type of data I am sending you, followed by the type.

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 console.log ("Server is running");})

Notice that there are many types here.

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 ("Hello World hello world");}}); server.listen (3000 focus 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 on "how to solve the problem of Chinese garbled code in client request data by node.js". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report