1

I'm making website using django and react. I have already set up backend in django and tested it using postman, everything works fine but when I'm doing exact same thing using react i'm getting 400 error.

Tried to do it like that:

To avoid any other error I used static data in this example

export default function MakePost(){
    
    
    const [userID, setUserID] = useState(1);
    const [text, setText] = useState('');

    
    const submit = async e => {
        e.preventDefault();
            
        
        const requestOptions = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({id: 1, text: 'some text'})
        }
        
        fetch('http://localhost:8000/api/make-post/', requestOptions)
        .then(response => {
            console.log(response)
            if(response.ok){
                return response.json();
            }
            
        });
        
        window.location.reload();
    }
Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
  • This is probably a CORS issue. Add a catch() method to the fetch Promise chain and console.log the error to get more details. This might be relevant to your issue: https://stackoverflow.com/questions/44037474/cors-error-while-consuming-calling-rest-api-with-react – Kostas Minaidis Jul 09 '23 at 01:39

0 Answers0