0

I have a react frontend that is making a request to my nodejs express backend. Cors is enabled like so

import express, { Express } from 'express'
import cors from 'cors'
import todoRoutes from './routes/router'

const app: Express = express()

const PORT: string | number = process.env.PORT || 4000

app.use(cors())
app.use(express.json())

However, when I make the request I nonetheless get this error back

index.js:1 Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:91)

My request looks like this

const addTodo = async (
    formData: ITodo
): Promise<void> => {
    const todo: Omit<ITodo, '_id'> = {
      name: formData.name,
      text: formData.text,
      status: false,
      createdAt: formData.createdAt
      }
    return await axios.post(
        api + '/add-todo', {
          headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
          },
          todo
        }
    ).then((response => {
          console.log(response)

        }
    ), (error) => {
      console.error(error)
      throw new Error(error)
    })

In the Network tab on devtools it just says for the status blocked:other

Nespony
  • 857
  • 3
  • 14
  • 27
  • I hope this answer can help you https://stackoverflow.com/questions/44617825/passing-headers-with-axios-post-request. – Amit Tiwary Sep 24 '20 at 09:23
  • not helpful please attach what error message says in your question – Swapnil Soni Sep 24 '20 at 09:46
  • What does the devtools console log say? The Network tab? these are generally quite explicit about CORS problems. Also, you may need to handle so-called preflight OPTIONS requests from your browser. Read this: https://www.npmjs.com/package/cors#enabling-cors-pre-flight – O. Jones Sep 24 '20 at 10:00
  • @O.Jones it just says 'index.js:1 Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:91)'. On the network tab it simply says 'blocked:other' – Nespony Sep 24 '20 at 11:51
  • @SwapnilSoni the whole error is this 'index.js:1 Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:91)'. On the network tab it simply says 'blocked:other' – Nespony Sep 24 '20 at 11:52

0 Answers0