[英]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.