0
点赞
收藏
分享

微信扫一扫

ajax 的 response 里的 Content-Type 编码格式好像不能改


因为需要将 charset 设置成 sjis 编码格式,但是charset 不知为什么怎么设置好像都是 utf-8

ajax 的 response 里的 Content-Type 编码格式好像不能改_charset

原来是因为我用了 express 搭建的后台,用 PHP 框架 Symfony 就不会有这个问题。下面是 express 的代码。
express 为什么不好使,暂时不清楚。

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>AJAX</title>
  </head>
  <body>
    <h2>AJAX</h2>
    <button type="button" onclick="loadXMLDoc()">请求数据</button>
    <div id="myDiv"></div>

    <script type="text/javascript">
      function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp = new XMLHttpRequest();
        } else {
          // code for IE6, IE5
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
          }
        };
        xmlhttp.open("GET", "/ajax", true);
        xmlhttp.send();
      }
    </script>
  </body>
</html>

index.js

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/public/index.html");
});

app.get("/ajax", (req, res) => {
  res.header({
    "Content-Type": "application/json; charset=SJIS"
  });
  res.send('你好');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

 

举报

相关推荐

0 条评论