久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <bdo id='ZFVb6'></bdo><ul id='ZFVb6'></ul>
  • <tfoot id='ZFVb6'></tfoot>
    <legend id='ZFVb6'><style id='ZFVb6'><dir id='ZFVb6'><q id='ZFVb6'></q></dir></style></legend>

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

      1. <i id='ZFVb6'><tr id='ZFVb6'><dt id='ZFVb6'><q id='ZFVb6'><span id='ZFVb6'><b id='ZFVb6'><form id='ZFVb6'><ins id='ZFVb6'></ins><ul id='ZFVb6'></ul><sub id='ZFVb6'></sub></form><legend id='ZFVb6'></legend><bdo id='ZFVb6'><pre id='ZFVb6'><center id='ZFVb6'></center></pre></bdo></b><th id='ZFVb6'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ZFVb6'><tfoot id='ZFVb6'></tfoot><dl id='ZFVb6'><fieldset id='ZFVb6'></fieldset></dl></div>
      2. 獲取 POST 請求中的空正文

        Empty body in fetch POST request(獲取 POST 請求中的空正文)
        <legend id='Lw3ZB'><style id='Lw3ZB'><dir id='Lw3ZB'><q id='Lw3ZB'></q></dir></style></legend>
          <tbody id='Lw3ZB'></tbody>

          <tfoot id='Lw3ZB'></tfoot>

        1. <small id='Lw3ZB'></small><noframes id='Lw3ZB'>

        2. <i id='Lw3ZB'><tr id='Lw3ZB'><dt id='Lw3ZB'><q id='Lw3ZB'><span id='Lw3ZB'><b id='Lw3ZB'><form id='Lw3ZB'><ins id='Lw3ZB'></ins><ul id='Lw3ZB'></ul><sub id='Lw3ZB'></sub></form><legend id='Lw3ZB'></legend><bdo id='Lw3ZB'><pre id='Lw3ZB'><center id='Lw3ZB'></center></pre></bdo></b><th id='Lw3ZB'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Lw3ZB'><tfoot id='Lw3ZB'></tfoot><dl id='Lw3ZB'><fieldset id='Lw3ZB'></fieldset></dl></div>
          • <bdo id='Lw3ZB'></bdo><ul id='Lw3ZB'></ul>

                1. 本文介紹了獲取 POST 請求中的空正文的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在為 Javascript 中的 fetch API 苦苦掙扎.當(dāng)我嘗試使用 fetch 方法將某些內(nèi)容發(fā)布到我的服務(wù)器時(shí),請求正文包含一個(gè)空數(shù)組.但是當(dāng)我使用 Postman 時(shí),它可以工作.這是我在 Node.js 中的服務(wù)器端代碼:

                  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 在服務(wù)器端是空的.

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

                  推薦答案

                  問題是

                  mode: 'no-cors'
                  

                  來自文檔...

                  防止方法成為除 HEAD、GET 或 POST 之外的任何東西,并且防止標(biāo)頭成為除 簡單標(biāo)題

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

                  簡單內(nèi)容類型標(biāo)題限制允許

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

                  這會使您精心設(shè)計(jì)的 Content-Type: application/json 標(biāo)頭變?yōu)?content-type: text/plain(至少在通過 Chrome 測試時(shí)).

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

                  由于您的 Express 服務(wù)器需要 JSON,它不會解析此請求.

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

                  我建議省略 mode 配置.這將使用默認(rèn)的 "cors" 選項(xiàng).

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

                  由于您的請求不是 簡單,您可能需要添加一些 CORS 中間件您的 Express 服務(wù)器.

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

                  另一個(gè)(有點(diǎn)老套)選項(xiàng)是告訴 Express 將 text/plain 請求解析為 JSON.這允許您將 JSON 字符串作為簡單請求發(fā)送,這也可以避免飛行前 OPTIONS 請求,從而降低整體網(wǎng)絡(luò)流量...

                  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 最終代碼塊中添加了結(jié)束括號.

                  Added ending parenthesis to app.use final code block.

                  這篇關(guān)于獲取 POST 請求中的空正文的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調(diào)用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)

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

                    <tbody id='wQvBj'></tbody>
                  • <bdo id='wQvBj'></bdo><ul id='wQvBj'></ul>
                    <legend id='wQvBj'><style id='wQvBj'><dir id='wQvBj'><q id='wQvBj'></q></dir></style></legend><tfoot id='wQvBj'></tfoot>

                  • <i id='wQvBj'><tr id='wQvBj'><dt id='wQvBj'><q id='wQvBj'><span id='wQvBj'><b id='wQvBj'><form id='wQvBj'><ins id='wQvBj'></ins><ul id='wQvBj'></ul><sub id='wQvBj'></sub></form><legend id='wQvBj'></legend><bdo id='wQvBj'><pre id='wQvBj'><center id='wQvBj'></center></pre></bdo></b><th id='wQvBj'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='wQvBj'><tfoot id='wQvBj'></tfoot><dl id='wQvBj'><fieldset id='wQvBj'></fieldset></dl></div>

                          • 主站蜘蛛池模板: 91欧美精品成人综合在线观看 | 国产成人免费视频网站高清观看视频 | 精品视频一区二区 | 中文字幕视频在线观看 | 欧美在线成人影院 | 欧美1区| 精品久久精品 | 成人片在线看 | 亚州成人 | 亚洲视频一区二区三区 | 91精品久久久久久久久中文字幕 | 99久久久久国产精品免费 | 午夜视频免费在线观看 | 亚洲精品免费在线 | 亚洲日本视频 | 亚洲精品自在在线观看 | 亚洲国产欧美精品 | 国产一区二区三区精品久久久 | 日韩一区二区免费视频 | 欧美亚洲国产精品 | 久久久99精品免费观看 | 国产成人影院 | 久久精品国产亚洲夜色av网站 | 久久99久久| 国产在线a | 欧美一二精品 | 亚洲色图综合 | 日本免费在线 | 精品久久久久久久久久久久久久久久久 | 中文字幕亚洲免费 | 最新国产福利在线 | 91在线导航 | 天天精品在线 | 干干干日日日 | 一级毛片成人免费看a | 一区二区高清 | 亚洲一区二区不卡在线观看 | 91免费看片 | 日韩一区二区三区在线 | 国产一区 在线视频 | 欧美视频网 |