简体   繁体   中英

Localhost express mongodb server, fetch request to localhost returns 400 status

const data = {
    email: "paul@Smith.com",
    password: "PaulPass"
}
const body = JSON.stringify(data)
const paramsPost = {
   method: "POST",
    body: body
}
async function fetchContent() {
    console.log(paramsPost)
  let res = await fetch("http://localhost:3000/api/user/login", paramsPost);
  console.log(res)

When I make run this function and fetch the response the console log prints out:

Response {type: "cors", url: "http://localhost:3000/api/user/login", redirected: false, status: 400, ok: false, …}

Could you tell me why it is doing this? What did I do wrong? When I make the request on postman the request gives me a status 200 and returns a JWT.

Thank you,

Snow

Changing the params to this:

const paramsPost = {
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    method: "POST",
    body: body
  }

gives status 200!

I guess this is the answer but its very unsatisfying as what I wanted was the res.header

It's still undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM