簡體   English   中英

當我發出 Post 請求時,React Native 返回 [未處理的承諾拒絕:類型錯誤:網絡請求失敗]

[英]React Native returns [Unhandled promise rejection: TypeError: Network request failed] when I'm making a Post Request

當我使用 Axios 在 React Native 中發出發布請求時,它返回 [未處理的承諾拒絕:類型錯誤:網絡請求失敗]。 這是我的 json 和我的 axios 方法

const credentials= {
NickName: "ricardo.luna",
Password: "123",
AccesoAplicacion: 1,
DerechosRangoInicial: 1000,
DerechosRangoFinal: 1012
}

const loginAxios = () => {
axios
  .post('x.x.x.x/API/users', credentials)
  .then(response => {
    console.log(response.IdUser);
  });
 };

只需在.catch(error=>{}) () .catch(error=>{})以處理拒絕並捕獲返回的錯誤

您需要將您的帖子參數作為FormData傳遞

let bodyFormData = new FormData();

bodyFormData.set('NickName', 'Fred');
bodyFormData.set('Password', '123');
bodyFormData.set('AccesoAplicacion', 1);
bodyFormData.set('DerechosRangoInicial', 1000);
bodyFormData.set('DerechosRangoFinal', 1012);

const loginAxios = () => {
axios({
    method: 'post',
    url: 'x.x.x.x/API/users',
    data: bodyFormData,
    headers: {'Content-Type': 'multipart/form-data' }
    })
    .then(function (response) {
        //handle success
        console.log(response);
    })
    .catch(function (response) {
        //handle error
        console.log(response);
    });
};

或者您可以使用querystring模塊來構建您的查詢字符串

const querystring = require('querystring');

const credentials= {
NickName: "ricardo.luna",
Password: "123",
AccesoAplicacion: 1,
DerechosRangoInicial: 1000,
DerechosRangoFinal: 1012
}

const loginAxios = () => {
axios
  .post('x.x.x.x/API/users', querystring.stringify(credentials))
  .then(response => {
    console.log(response.IdUser);
  });
 };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM