search

Home  >  Q&A  >  body text

node.js - node中JSON.stringfy中文乱码?

提交表单后浏览器看字符串中文乱码,希望大神提供解决方法

index.htm 文件代码如下:

<html>
<body>
<form action="http://127.0.0.1:8081/process_get" method="GET">
First Name: <input type="text" name="first_name">  <br>

Last Name: <input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
</body>
</html>

server.js 文件代码如下:

var express = require('express');
var app = express();

app.use(express.static('public'));

app.get('/index.htm', function (req, res) {
   res.sendFile( __dirname + "/" + "index.htm" );
})

app.get('/process_get', function (req, res) {

   // 输出 JSON 格式
   response = {
       first_name:req.query.first_name,
       last_name:req.query.last_name
   };
   console.log(response);
   res.end(JSON.stringify(response));
})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("应用实例,访问地址为 http://%s:%s", host, port)

})
大家讲道理大家讲道理2780 days ago515

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 15:01:52

    Is the browser encoding UTF-8?

    reply
    0
  • PHPz

    PHPz2017-04-17 15:01:52

    app.get('/process_get', function (req, res) {

    // 输出 JSON 格式
    response = {

       first_name:req.query.first_name,
       last_name:req.query.last_name

    };
    console.log(response);

    //使用这行代码

    res.end( JSON.stringify( response ) ,'utf-8');
    })

    reply
    0
  • Cancelreply