首页 > 解决方案 > 不能通过使用 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);
  });
}

任何帮助,将不胜感激。

标签: react-nativeaxios

解决方案


我今天被困在同一个问题上几个小时。据我了解,你不能通过 axious 发送带有 body 的 GET 请求。只需在您的服务器中将 GET 方法更改为 POST 即可。您可以检查此以选择我在哪里发布有关此问题的更详细答案。


推荐阅读