【问题标题】:react axios not providing a response反应 axios 不提供响应
【发布时间】:2021-01-25 12:14:29
【问题描述】:

我的代码如下:

import axios from 'axios';

const getBannerStatus = () => {     
    let url = `http://localhost:3000` + apiConstants.API_POST_BANNERSTATUS;   
    axios.post(url)    
        .then(response => {
            console.log("axios response = " + response);
            return response.data.bannerStatus;
        });     
}

我已经使用 chrome 开发工具进行了测试,它正在进入 axios 模块。我已经测试了 URL 并获得了 JSON 又名 response.data 在 POSTMAN 中没有问题。

从 POSTMAN 调用返回的 JSON。

{
    "bannerMessage": null,
    "bannerHeader": null,
    "result": "success",
    "bannerStatus": "HIDE"
}

但是 axios 代码没有返回响应,实际上它甚至没有提供console.log("axios response = " + response);

谁能告诉我我错过了什么?

【问题讨论】:

    标签: axios


    【解决方案1】:

    问题是缺少标题信息。以下代码正在运行。

    const getBannerStatus = () => {
      let url = `http://localhost:3000` + apiConstants.API_POST_BANNERSTATUS;
      axios.post(url,{},{
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            "Cache-Control": "no-store, no-cache, must-revalidate",
            pragma: "no-cache",
            Expires: 0,
          }
          })
        .then((response) => {
          console.log("axios response = " + response.data.bannerStatus);
          return response.data.bannerStatus;
        });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多