【问题标题】:Why does the result of http request return an empty array?为什么http请求的结果返回一个空数组?
【发布时间】:2021-10-07 15:28:22
【问题描述】:

我尝试使用 axiosfetch 从 API 端点获取数据,但结果只返回了一个空数组。它在邮递员中运行良好。我错过了什么?

使用 Axios

axios({
        method: "get",
        url: "/api/purchase/receivegoods/index?action=detail-grn",
        params: {
          goods_receive_id: 71,
        },
        headers: {
          "Content-Type": "application/json",
          Accept: "application/json",
        },
      })
        .then((response) => {
          console.log(("response", response));
        })
        .catch((error) => {
          console.log("error message: ", error.message);
        });

使用 Fetch

fetch(
        "https://devactive.com/api/purchase/receivegoods/index?action=detail-grn",
        {
          method: "GET",
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
          },
          params: {
            goods_receive_id: 71,
          },
        }
      )
        .then((response) => response.json())
        .then((result) => {
          console.log("Success:", result);
        })
        .catch((error) => {
          console.error("Error:", error);
        });

编辑: 请求采用 JSON 格式。它看起来像这样(这是现有 id 的示例:

{
    "goods_receive_id" : 71
}

【问题讨论】:

  • 您在 Postman 中提出的请求是什么样的?
  • json 格式。它只发送一个 json 格式的 id。

标签: axios httprequest httpresponse fetch-api


【解决方案1】:

检查可能的代理配置,邮递员默认使用系统代理。

【讨论】:

    【解决方案2】:

    在使用 Axios 或 Fetch 时,您不会将数据作为正文传递。不过,您正在达到终点。试试这样的:

    data = {
      "goods_receive_id" : 71
    }
    
    axios({
            method: "get",
            url: "/api/purchase/receivegoods/index?action=detail-grn",
            data: JSON.stringify(data), // <- You add the data to the body here
            headers: {
              "Content-Type": "application/json",
              Accept: "application/json",
            },
          })
            .then((response) => {
              console.log(("response", response));
            })
            .catch((error) => {
              console.log("error message: ", error.message);
            });
    

    data = {
      "goods_receive_id" : 71
    }
    fetch(
            "https://devactive.com/api/purchase/receivegoods/index?action=detail-grn",
            {
              method: "GET",
              headers: {
                Accept: "application/json",
                "Content-Type": "application/json",
              },
              data: JSON.stringify(data), // <- You add the data to the body here
            }
          )
            .then((response) => response.json())
            .then((result) => {
              console.log("Success:", result);
            })
            .catch((error) => {
              console.error("Error:", error);
            });
    

    【讨论】:

    • 我尝试过使用 JSON.stringify。但这行不通。
    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 2018-10-25
    • 2016-07-22
    • 2021-01-25
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多