react-native - 不能通过使用 axios 将 get 与 body 参数一起使用
问题描述
我使用邮递员成功测试了它。我得到状态码 200。
在 React Native 0.60.5 中使用 Axios
但是当我在 react native 项目上测试它时,我得到了Error: Request failed with status code 415
我不知道为什么。
这是我的 axios 代码:
ApiServices.ts
import axios from 'axios';
const instance = axios.create({
baseURL: 'http://192.168.1.161:5000/',
timeout: 3000,
});
instance.interceptors.request.use((config) => {
return config;
}, (error) => {
// handle error
return Promise.reject(error);
});
instance.interceptors.response.use((response) => {
return response;
}, error => {
// handle error
return Promise.reject(error);
});
export const Get = async (api: string, params: object) => {
console.log('api', api); // Account
console.log('params', params); // { userAccount: "0989257556", userPwd: "12345" }
return new Promise((resolve, reject) => {
instance.get(api, params).then(res => {
resolve(res.data);
}).catch(error => {
reject(error);
});
})
};
我使用这样的功能:
export const testApiGet = () => {
const userAccount = '0989257556';
const userPwd = '12345';
Get('Account', { userAccount, userPwd }).then(res => {
console.log('res ->', res);
}).catch(err => {
console.log('err ->', err);
});
}
任何帮助,将不胜感激。
解决方案
我今天被困在同一个问题上几个小时。据我了解,你不能通过 axious 发送带有 body 的 GET 请求。只需在您的服务器中将 GET 方法更改为 POST 即可。您可以检查此以选择我在哪里发布有关此问题的更详细答案。
推荐阅读
- typescript - 在 TypeScript 中使用 useNewUrlParser 和 mongoose 输入错误
- python - os.system('cls') 不清除 Pycharm 中的屏幕?
- php - 我想在 php 条件下调用 {{$barang->stock}} 并返回文本值
- swift - 快速设置多个 TableView 高度
- javascript - 如何修复“for”循环中的“let i = 0”值在完成循环后再次变为0?
- ruby - 如何在 Ruby 中使用 require?
- node.js - 在一种方法中找不到 TypeORM 存储库,但在另一种方法中找到
- perl - 如何在 Perl 中处理可变维度数组
- android - 有没有办法将移动响应网页重定向到桌面版本?
- javascript - 导入函数的未定义值