gpt4 book ai didi

javascript - React Native 返回 [未处理的 promise 拒绝 : TypeError: Network request failed] when I'm making a Post Request

转载 作者:行者123 更新时间:2023-11-28 03:04:14 25 4
gpt4 key购买 nike

当我使用 Axios 在 React Native 中发出发布请求时,它会返回 [未处理的 promise 拒绝:类型错误:网络请求失败]。这是我的 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);
});
};

最佳答案

您需要将帖子参数传递为 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);
});
};

关于javascript - React Native 返回 [未处理的 promise 拒绝 : TypeError: Network request failed] when I'm making a Post Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60747406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com