首页 > 解决方案 > Axios 使用 Reactjs 发布 - 春季启动

问题描述

我对 Axios 很陌生,我尝试使用 Axios 进行 POST 请求,而我的数据库没有获取数据,它创建了一个实体,除了主键为 0 之外,所有值都为空。然后,我收到一个错误我正在复制主键。我不知道它是否没有得到我的 JSON 或者我做错了什么。

api.js

import axios from "axios";

const endpoints = {
 development: 'http://localhost:8080/',
};

export const api = axios.create({
baseURL: endpoints['development'],
timeout: 20000,
 headers: {"Content-type":"application/json" }
});

pacienteService.js

import { api } from './helpers/api.js';

const basePath = 'api';

let config = {
    headers: {
       'Content-Type': 'application/json',
    } 
}

function getAll() { return api.get(`${basePath}/pacientes`); }

function show(pacienteId) { return api.get(`${basePath}/?id=${pacienteId}`); }

function create(data) { return api.post(`${basePath}/pacientes`, data,config); }

const pacientesService = { getAll, show, create };
export default pacientesService;

提交表单时发布的功能

handleSubmit(event){
    const paciente = {rut:this.state.rut,
    nombre:this.state.nombre ,
    nacionalidad:this.state.nombre ,
    sexo:this.state.nombre ,
    fecha_na:this.state.nombre ,
    domicilio:this.state.nombre ,
    diagnostico:this.state.nombre ,
    telefono:this.state.nombre ,
    gravedad:this.state.recuperacion
    }
      
       pacientesService.create({paciente}).then(res => {
        console.log(res);
        console.log(res.data);
      })
}

Pd:我已经打印了所有的状态,看看它们是否正确,它们是正确的。

标签: javascriptreactjsaxios

解决方案


修复了将数据直接传递到pacientesService.create().

pacientesService.create({paciente}), 到
pacientesService.create({rut:this.state.rut,nombre:this.state.nombre,... })


推荐阅读