简体   繁体   English

当我发出 Post 请求时,React Native 返回 [未处理的承诺拒绝:类型错误:网络请求失败]

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

When I make a post request in React Native with Axios it returns a [Unhandled promise rejection: TypeError: Network request failed].当我使用 Axios 在 React Native 中发出发布请求时,它返回 [未处理的承诺拒绝:类型错误:网络请求失败]。 This is my json and my axios method这是我的 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=>{})以处理拒绝并捕获返回的错误

You need to pass your post params as FormData您需要将您的帖子参数作为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);
    });
};

Or you can use querystring module to build your query string或者您可以使用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.

相关问题 Axios 承诺处理 - 在 react-native 中获取“可能未处理的承诺拒绝 - 类型错误:网络请求失败” - Axios Promise Handling - Getting “Possible Unhandled Promise Rejection - TypeError: Network request failed” in react-native React Native:可能未处理的承诺拒绝(id:0)TypeError:网络请求失败 - React Native: Possible unhandled promise rejection (id: 0) TypeError: Network request failed React Native 获取错误:“[未处理的 promise 拒绝:TypeError:网络请求失败]” - React Native fetch error: “[Unhandled promise rejection: TypeError: Network request failed]” react-native 错误:[未处理的 promise 拒绝:错误:获取 Expo 令牌时遇到错误:TypeError:网络请求失败。] - react-native error:[Unhandled promise rejection: Error: Error encountered while fetching Expo token: TypeError: Network request failed.] POST 请求... UnhandledPromiseRejectionWarning:未处理的承诺拒绝 - POST request... UnhandledPromiseRejectionWarning: Unhandled promise rejection 在 React-Native 中尝试 POST 请求时网络请求失败 - Network request failed when trying POST request in React-Native 可能未处理的承诺拒绝(ID:1):类型错误:网络请求失败 - Possible Unhandled Promise Rejection (id: 1): TypeError: Network re quest failed React Native TypeError:网络请求失败,fetch() - React native TypeError: Network request failed with fetch() React Native中未处理的承诺拒绝 - Unhandled promise rejection in react native React Native:可能的未处理的承诺拒绝(id:0):TypeError:undefined不是对象 - React Native: Possible Unhandled Promise Rejection (id: 0): TypeError: undefined is not an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM