首页 > 解决方案 > 数据未从 React Native 发送到 Spring Boot API 的 Axio 发布请求

问题描述

当我尝试从 React 本机向我的 Spring Boot API 发出 axio 发布请求时,我的数据没有被发送?

这是我的apiReq函数代码:

const apiReq = () => {
  return (axios.post('http://{IP address}:8082/api/login', {

    headers: {
      "Accept": 'application/json',
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      "password": "#####",
      "username": "abcd.gmail.com"
    })
  })
    .then(response => {

      console.log(response.data.responseMessage.errorCode);

    })
    .then(result => console.log(result))
    .catch(error => console.log('error', error))
  )

}

标签: mysqlspring-bootreact-nativepostaxios

解决方案


也许,您不使用 JSON stringfy。另外,我使用这样的axios。

const body = {
      password: "#####",
      username: "abcd.gmail.com"
    }
const options = {
    headers: {
          "Accept": 'application/json',
          "Content-Type": "application/json"
    }
}

axios.post('http://{IP address}:8082/api/login', body, options)
.then(response => console.log(response))
.catch(err => console.log(err));

推荐阅读