English 中文(简体)
NextJS 13 Line Handler 请求听询
原标题:NextJS 13 RouteHandler Request Invalid JSON

Currently using the next Next.JS 13 Route Handler to process information passed by a form. And I m trying to figure out why I can t get the new Route Handler to function despite mirroring the doc (above link) code.

export async function POST( request: Request) {
    console.log( Body , request.body)
    const res = await request.json();
    ...
}

But when trying to process the request data, I get an error:

Body ReadableStream { locked: false, state:  readable , supportsBYOB: false }
error - SyntaxError: Unexpected token  d , "dqaf=ll" is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:6444:19)
    at successSteps (node:internal/deps/undici/undici:6418:27)
    at node:internal/deps/undici/undici:1133:60
    at node:internal/process/task_queues:140:7
    at AsyncResource.runInAsyncScope (node:async_hooks:204:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Request raw body sent by postman: dqaf=ll

页: 1

我将手稿改成一个皮路,载于/pages/api/contactForm.j。 (并拆除了预防冲突的路手)看来行之有效:

export default function handler(req, res) {
    const body = req.body
    console.log( body:  , body)
}

Logs:

body:  dqaf=ll

我在路边做一些不正确的事情吗?

I ve tried turning off the bodyParser by including the following code in 。 but I still get the same syntax error:

export const config = {
    api: {
        bodyParser: false,
    }
}

Logs:

error - SyntaxError: Unexpected token  d , "dqaf=ll" is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:6444:19)
    at successSteps (node:internal/deps/undici/undici:6418:27)
    at node:internal/deps/undici/undici:1133:60
    at node:internal/process/task_queues:140:7
    at AsyncResource.runInAsyncScope (node:async_hooks:204:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

我最后将采用<代码>/pages/api目前处理路线的格式,但如果我从路上找不到东西,或者如果这是用新的第13条 next子的ug子,我会想理解吗?

问题回答

在试图绕过Nodejs / Express POST 要求处理路线后,将同一问题转至React服务器侧边/下游应用路线。

export async function POST(req) {
   let res = await request.json() // gave me the same error.
   let formData = await request.formData()  // working
   let text = await request.text() // working

I definitely was not receiving form data but that did the trick for me. in express I had been using the follwing to handle req.body.json(),

var bodyParser = require( body-parser )
const urlencoded = require( body-parser ).urlencoded; // For Twillio Response.

将表格数据归入标准联合材料,如此。

  const formData = await request.formData()
  let data = {
    city: formData.get( CallerCity ),
    state: formData.get( CallerState ),
  }




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签