• <legend id='xUhjN'><style id='xUhjN'><dir id='xUhjN'><q id='xUhjN'></q></dir></style></legend>
    <i id='xUhjN'><tr id='xUhjN'><dt id='xUhjN'><q id='xUhjN'><span id='xUhjN'><b id='xUhjN'><form id='xUhjN'><ins id='xUhjN'></ins><ul id='xUhjN'></ul><sub id='xUhjN'></sub></form><legend id='xUhjN'></legend><bdo id='xUhjN'><pre id='xUhjN'><center id='xUhjN'></center></pre></bdo></b><th id='xUhjN'></th></span></q></dt></tr></i><div id='xUhjN'><tfoot id='xUhjN'></tfoot><dl id='xUhjN'><fieldset id='xUhjN'></fieldset></dl></div>

      <bdo id='xUhjN'></bdo><ul id='xUhjN'></ul>
    <tfoot id='xUhjN'></tfoot>

        <small id='xUhjN'></small><noframes id='xUhjN'>

        获取 POST 请求中的空正文

        Empty body in fetch POST request(获取 POST 请求中的空正文)

          1. <tfoot id='tJ3vX'></tfoot>

              <small id='tJ3vX'></small><noframes id='tJ3vX'>

              <i id='tJ3vX'><tr id='tJ3vX'><dt id='tJ3vX'><q id='tJ3vX'><span id='tJ3vX'><b id='tJ3vX'><form id='tJ3vX'><ins id='tJ3vX'></ins><ul id='tJ3vX'></ul><sub id='tJ3vX'></sub></form><legend id='tJ3vX'></legend><bdo id='tJ3vX'><pre id='tJ3vX'><center id='tJ3vX'></center></pre></bdo></b><th id='tJ3vX'></th></span></q></dt></tr></i><div id='tJ3vX'><tfoot id='tJ3vX'></tfoot><dl id='tJ3vX'><fieldset id='tJ3vX'></fieldset></dl></div>
              <legend id='tJ3vX'><style id='tJ3vX'><dir id='tJ3vX'><q id='tJ3vX'></q></dir></style></legend>

                  <bdo id='tJ3vX'></bdo><ul id='tJ3vX'></ul>
                    <tbody id='tJ3vX'></tbody>
                  本文介绍了获取 POST 请求中的空正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在为 Javascript 中的 fetch API 苦苦挣扎.当我尝试使用 fetch 方法将某些内容发布到我的服务器时,请求正文包含一个空数组.但是当我使用 Postman 时,它可以工作.这是我在 Node.js 中的服务器端代码:

                  I'm struggling with the fetch API in Javascript. When I try to POST something to my server with fetch method, the request body contains an empty array. But when I use Postman it works. Here is my server-side code in Node.js:

                  const express = require('express')
                  const app = express()
                  const port = 3000
                  
                  app.use(express.json())
                  app.post('/api', function (req, res) {
                      console.log(req.body)
                  })
                  app.listen(port)
                  

                  这是我的客户端代码:

                  fetch('http://"theserverip":3000/api', {
                      method: 'POST',
                      headers: { "Content-Type": "application/json" },
                      mode: 'no-cors',
                      body: JSON.stringify({
                          name: 'dean',
                          login: 'dean',
                      })
                  })
                  .then((res) => {
                      console.log(res)
                  })
                  

                  问题是 req.body 在服务器端是空的.

                  The problem is that the req.body is empty on server side.

                  推荐答案

                  问题是

                  mode: 'no-cors'
                  

                  来自文档...

                  防止方法成为除 HEAD、GET 或 POST 之外的任何东西,并且防止标头成为除 简单标题

                  Prevents the method from being anything other than HEAD, GET or POST, and the headers from being anything other than simple headers

                  简单内容类型标题限制允许

                  • 文本/纯文本,
                  • application/x-www-form-urlencoded,以及
                  • multipart/form-data

                  这会使您精心设计的 Content-Type: application/json 标头变为 content-type: text/plain(至少在通过 Chrome 测试时).

                  This causes your nicely crafted Content-Type: application/json header to become content-type: text/plain (at least when tested through Chrome).

                  由于您的 Express 服务器需要 JSON,它不会解析此请求.

                  Since your Express server is expecting JSON, it won't parse this request.

                  我建议省略 mode 配置.这将使用默认的 "cors" 选项.

                  I recommend omitting the mode config. This uses the default "cors" option instead.

                  由于您的请求不是 简单,您可能需要添加一些 CORS 中间件您的 Express 服务器.

                  Since your request is not simple, you'll probably want to add some CORS middleware to your Express server.

                  另一个(有点老套)选项是告诉 Express 将 text/plain 请求解析为 JSON.这允许您将 JSON 字符串作为简单请求发送,这也可以避免飞行前 OPTIONS 请求,从而降低整体网络流量...

                  Another (slightly hacky) option is to tell Express to parse text/plain requests as JSON. This allows you to send JSON strings as simple requests which can also avoid a pre-flight OPTIONS request, thus lowering the overall network traffic...

                  app.use(express.json({
                    type: ['application/json', 'text/plain']
                  }))
                  

                  app.use 最终代码块中添加了结束括号.

                  Added ending parenthesis to app.use final code block.

                  这篇关于获取 POST 请求中的空正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                  XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                  Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                  How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                  How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
                  quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
                  <legend id='BggyC'><style id='BggyC'><dir id='BggyC'><q id='BggyC'></q></dir></style></legend>
                  <tfoot id='BggyC'></tfoot>
                  • <bdo id='BggyC'></bdo><ul id='BggyC'></ul>

                        <small id='BggyC'></small><noframes id='BggyC'>

                            <tbody id='BggyC'></tbody>
                        1. <i id='BggyC'><tr id='BggyC'><dt id='BggyC'><q id='BggyC'><span id='BggyC'><b id='BggyC'><form id='BggyC'><ins id='BggyC'></ins><ul id='BggyC'></ul><sub id='BggyC'></sub></form><legend id='BggyC'></legend><bdo id='BggyC'><pre id='BggyC'><center id='BggyC'></center></pre></bdo></b><th id='BggyC'></th></span></q></dt></tr></i><div id='BggyC'><tfoot id='BggyC'></tfoot><dl id='BggyC'><fieldset id='BggyC'></fieldset></dl></div>