简体   繁体   English

Express:从 Content-Type "application/json; charset=utf-8" 中删除 charset=utf-8

[英]Express: Remove charset=utf-8 from Content-Type "application/json; charset=utf-8"

I have a NodeJS and express based app.我有一个基于 NodeJS 和 express 的应用程序。 Every time I m trying to fetch a response, I m getting Content-Type: "application/json; charset=utf-8" .每次我尝试获取响应时,都会得到Content-Type: "application/json; charset=utf-8" I m unable to parse this on front end as I m expecting response with header Content-Type: "application/json" .我无法在前端解析这个,因为我期待响应头Content-Type: "application/json"

I have tried res.setHeader, res.set methods as well but nothing seems to be helpful.我也尝试过res.setHeader, res.set方法,但似乎没有任何帮助。 Any advice is appreciated.任何建议表示赞赏。

Following is my express code:以下是我的快速代码:

const app = express();

configureMongoClient();

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.options('*', cors())

app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, X_BPI_CONTEXT');
    res.header("Content-Type", "application/json")
    next();
});
app.use("/users", usersRouter);
app.use(express.static(path.join(__dirname, "public")));

My Front end call is as follow:我的前端调用如下:

fetch(uri, {
    method: "POST",
    headers: {
      Content-Type: "application/json"
    },
    body: JSON.stringify(requestData),
  })
    .then((response) => {
      debugger;
      return response.json()
    }) .then((data) => {
        console.log(data);
      });

Express will set the charset for you. Express 将为您设置字符集。 So if you want to bypass it, don't use express methods, since res extends from: http.ServerResponse you can use .writeHeader & .write .所以如果你想绕过它,不要使用 express 方法,因为res扩展自: http.ServerResponse你可以使用.writeHeader & .write

The res object is an enhanced version of Node's own response object and supports all built-in fields and methods. res 对象是 Node 自己的响应对象的增强版,支持所有内置的字段和方法。

res.writeHead(200, { 'Content-Type': 'application/json' })
res.write(JSON.stringify(object))
res.end()

In any case, it's better to add the charset, and I suggest you do the changes in the front end instead.无论如何,最好添加字符集,我建议您改为在前端进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将 Content-Type 更改为“application/json;charset=UTF-8” - Unable to Change Content-Type to "application/json; charset=UTF-8" encodeURIComponent 和 content-type: 'charset: utf-8' - encodeURIComponent and content-type: 'charset: utf-8' 无法在Node js中使用“ content-type”:“ text / plain; charset = UTF-8” - Unable to Print a JSON Object in Node js with “content-type”: “text/plain; charset=UTF-8” 可怕的错误:错误的内容类型 header,未知的内容类型:text/plain;charset=UTF-8 - formidable error: bad content-type header, unknown content-type: text/plain;charset=UTF-8 将Content-Type的响应标头设置为“ application / json; charset = utf-16”将json转换为中文字符 - Setting response header for Content-Type as “application/json; charset=utf-16” converts json to chinese character 在快速响应中从Content-type中删除字符集 - remove charset from Content-type in express response 节点:Express:如何处理 application/octet-stream;charset=;UTF-8 响应? - Node: Express: How to handle application/octet-stream;charset=;UTF-8 response? nodejs 中的 ejs 视图引擎不显示 utf-8 字符集 - ejs view engine in nodejs doesn't show utf-8 charset Node.js公共静态文件夹,用于提供带有utf-8字符集的js - Node.js public static folder to serve js with utf-8 charset 将js编码标头表达为utf-8 - express js encode headers to utf-8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM