简体   繁体   English

CORS 400 Bad Request with fetch, express server

[英]CORS 400 Bad Request with fetch, express server

I'm trying to send a POST request from 127.0.0.1:8080 to my express server in localhost:3000/trips I'm having a lot of problem with the cors configuration我正在尝试将 POST 请求从 127.0.0.1:8080 发送到我在 localhost:3000/trips 中的快速服务器我在 cors 配置方面遇到了很多问题

First, this is my method to do the POST request首先,这是我执行 POST 请求的方法

   async modifyTrip() {
      let json = {
        data: "test",
        mezzo: "test",
        coordinate: ["test"],
        tappe: ["test"],
      };
      let modifyform = document.getElementById("add-form");
      modifyform.onsubmit = async (e) => {
        e.preventDefault();
        await fetch("http://localhost:3000/trips", {
          method: "POST",
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
          },
          body: json,
        });
      };
    }

On the server side if I put cors options at that point returns me that error:如果我在服务器端放置 cors 选项,则会返回该错误:

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(
  cors({
    "Access-Control-Allow-Origin": "*",
    "Allow-Methods": "GET, POST, DELETE, FETCH",
  })
);
app.use("/user", userRoutes);
app.use("/trips", tripsRoutes);

错误信息

If I try to change the position the error is different my it always gives me problem如果我尝试改变位置,错误会有所不同,我总是会给我带来问题

app.use(
  cors({
    "Access-Control-Allow-Origin": "*",
    "Allow-Methods": "GET, POST, DELETE, FETCH",
  })
);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/user", userRoutes);
app.use("/trips", tripsRoutes);

错误信息

I don't think the matter is of where I put it, but I can't fix this problem anyway.我不认为问题在于我把它放在哪里,但无论如何我都无法解决这个问题。 Maybe I have to change some headers in my client side, but i really can't figure it out也许我必须在客户端更改一些标题,但我真的想不通

Thank you.谢谢你。

It's a 400 Bad Request error, so look up what that means :这是一个 400 Bad Request 错误,所以请查看这意味着什么

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).超文本传输​​协议 (HTTP) 400 错误请求响应状态代码表示服务器不能或不会处理请求,因为某些东西被认为是客户端错误(例如,格式错误的请求语法、无效的请求消息帧或欺骗性)请求路由)。

So something is wrong with the request and the server is complaining (before it gets to the bit of code which would add the CORS response headers).所以请求出了点问题,服务器在抱怨(在它到达将添加 CORS 响应标头的代码之前)。 The CORS error is a side-effect , not the main problem. CORS 错误是副作用,而不是主要问题。

If you look at the Network tab of your browser's developer tools , you will be able to examine the request.如果您查看浏览器开发人员工具的网络选项卡,您将能够检查该请求。

The body will look something like this:身体看起来像这样:

[object Object]

Now, you said (using a Content-Type header) you were POSTing JSON, but [object Object] is not JSON (or even a usable representation of your data).现在,您说(使用 Content-Type 标头)您正在发布 JSON,但[object Object]不是 JSON(甚至不是您的数据的可用表示)。

You need to pass JSON to body and not an object.您需要将 JSON 传递给body而不是对象。 Since you are passing an object, it gets stringified using the default JS mechanism (which gives "[object Object]" ).由于您正在传递一个对象,因此它会使用默认的 JS 机制(提供"[object Object]" )进行字符串化。

Use JSON.stringify(your_object) to convert it to JSON.使用JSON.stringify(your_object)将其转换为 JSON。

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

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