redux无法加载资源:服务器的状态为400(错误请求)

时间:2020-06-30 11:46:08

标签: javascript reactjs react-redux react-hooks redux-thunk

我的代码正常工作,但是当我尝试从后端获取数据时,我收到一个错误“ 400错误请求”,不知道出了什么问题。请弄清楚我的问题。

// API == http:// localhost:8000 / api /

index.js

import {API} from '../backend';
import axios from 'axios';

export const getdata = () => {
    return () => {
        axios.post(`${API}addtodo`)
        .then(res => {
            console.log(res)
        })
      
        .catch(err =>{
            console.log(err);
        })
    }
}

我在这里共享执行添加操作的实际代码 Todoform.js

import React,{useState, useEffect} from 'react'
import '../App.css'
import {
    FormGroup,
    Input,
    Button,
    Form,
    InputGroup,
    InputGroupAddon
} from 'reactstrap';
import {v4} from 'uuid';
import 'bootstrap/dist/css/bootstrap.min.css';
import {getdata, AddTodo} from '../Auth'

//redux
import {connect, useDispatch} from 'react-redux'
import {addTodo} from '../Action/todo';

const TodoForm  = ({addTodo}) => {

    const [title,setTitle] = useState('')

    const dispatch = useDispatch();
 useEffect(() => {
        dispatch(getdata())
    }, []);


    return(
 <Form>
           <FormGroup>
               <InputGroup>
                    <Input 
                    type='text'
                    name='todo'
                    id='todo'
                    placeholder='Your next Todo'
                    value={title}
                    onChange={e => setTitle(e.target.value)}
                    />
                    <InputGroupAddon addonType='prepend'>
                        <Button color='primary' onClick={()=>{
                            if(title === ''){
                                return alert('please add a todo')
                            }
                            const todo = {
                                title,
                                id:v4(),
                            }
                    
                            addTodo(todo);
                    
                            setTitle('');
                        }}>
                        
                            ADD
                        </Button>
                    </InputGroupAddon>
               </InputGroup>
           </FormGroup>
       </Form>      
    )
}

const mapStateToProps = state => ({

}) 

const mapDispatchToProps = dispatch =>({
    addTodo : todo =>{
        dispatch(addTodo(todo))
    },
})


export default connect(mapStateToProps,mapDispatchToProps)(TodoForm)

$$$$$$$$$$$$$$后端$$$$$$$$$$$$$$$$$$$$$

todo.js /路由器

router.post('/addtodo',addTodo);

todo.js / controller

exports.addTodo = (req,res) =>{
  const todo = new Todo(req.body)
  todo.save((err,todo) => {
    if(err || !todo){
      return res.status(400).json({
          err : 'NOT able to store data in database'
      })
    }
    res.json(todo);
  })
}

1 个答案:

答案 0 :(得分:0)

将第二个参数传递给axios.post函数很重要。

由于您正在使用getData来获取数据,因此我对您的axios.post(`${API}addtodo`)函数仍然感到困惑。尝试在此路线上更改路线。

如果您有意尝试在没有正文的情况下请求addTodo路由,请添加{}作为第二个参数: axios.post(`${API}addTodo`, {})