【问题标题】:axios post is not working but same url is working with fetch apiaxios 帖子不起作用,但相同的 url 正在使用 fetch api
【发布时间】:2019-12-24 20:24:12
【问题描述】:

我在 react native 中使用 axios 发出一个 post 请求,除了在这个文件中它工作正常,为了测试我用 fetch api 替换它并且它工作正常。

我的代码中的第二行执行得很好,而第一行(使用 axios)没有,我在第一行和第三行注释了它,它打印了第一行的结果然后它可以工作,如果我取消注释它什么都不会打印(第一行停止下一行)

我确信 axios 已导入并可以正常工作,因为我有请求在同一个文件中工作。

//let response = await axios.post("http://testing.api.okatruck.com/api/customers/requestTripC",{})
let response2 = await fetch("http://testing.api.okatruck.com/api/customers/requestTripC",{method: 'POST'})
//console.log(response)
console.log(response2)

【问题讨论】:

    标签: react-native axios


    【解决方案1】:

    你可以试试:

    const url = "http://testing.api.okatruck.com/api/customers/requestTripC";
    const headers = {headers: { 
    "Authorization": `Token ${token}`, //edit this line according to the backend authorization
    "Content-Type": "application/json"
    }};
    const body = {};
    axios
    .post(url, body, headers)
    .then(res => {
      console.log("request received",res);
    })
    .catch(err => console.log("TODO: Handle error axios", err));
    

    【讨论】:

    • TODO: Handle error axios [Error: Request failed with status code 401] 至少它打印了一些东西,这里有什么区别?
    • 我可以知道您的 response2 是否显示了一些数据吗?您是否有某种令牌来访问数据?
    • response2 返回 401,这是正常的,因为我需要发送一个带有 headers 和我的数据的令牌
    猜你喜欢
    • 2018-03-01
    • 2020-02-29
    • 1970-01-01
    • 2021-09-10
    • 2019-01-26
    • 2020-10-20
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多