【问题标题】:Not receiving data on server when doing an API POST call from the client从客户端执行 API POST 调用时未在服务器上接收数据
【发布时间】:2021-05-09 19:36:52
【问题描述】:

已经用 Insomnia 检查了端点并且工作正常,但是当尝试从客户端连接后端时出现了某种问题。客户端和服务器之间的连接是这样完成的:

const uri = `${basePath}/${baseVersion}/sign-up`;
    const params = {
        method: "POST",
        body: JSON.stringify(data),
        header: {
            "Content-Type": "application/json"
        }
    };

如果我在控制台中显示 params 对象,这就是其中的内容:

enter image description here

澄清一下,不存在 CORS 问题,因为我使用的是 Google Chrome 扩展程序。

这是感染者的回应:

enter image description here

【问题讨论】:

  • 错误信息是什么?你能显示有效的请求调用而不是你的配置吗?
  • @R.LM 添加了 fetch 的响应
  • 响应看起来更像是请求而不是响应,你确定你添加了正确的图片?
  • @R.LM 你是对的!更新

标签: javascript api express


【解决方案1】:

你的问题是没有在承诺中收到服务器的响应吗?如果是这样,那是因为您的 sn-p 中没有实际返回数据的代码。 (对不起,如果我认错了问题,我没有能力发表评论)

const uri = `${basePath}/${baseVersion}/sign-up`;
 async function fetchPost(data = {}) {
   var response = await fetch(uri,
        method: "POST",
        mode: "cors",
        header: {
            "Content-Type": "application/json"
       },
       referrerPolicy: "strict-origin-when-cross-origin" //you can replace that with anything you want depending on the situation
       body: JSON.stringify(data)
    });
    // if you're expecting the response to be json, use the below, but if you want it in text, then do response.text, etc.
    return response.json(); 
    }
    fetchPost();

   
      

【讨论】:

    猜你喜欢
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多