简体   繁体   中英

Receiving error Unexpected token < in JSON at position 0; is this a backend or frontend problem?

I currently have a PATCH method:

export const patchEvent = (eventToUpdate) => dispatch => {
  fetch(`http://localhost:3000/events/${eventToUpdate.id}/`, {
      method: "PATCH",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        workers_hired: eventToUpdate.workers_hired
      })
  })
    .then(res => res.json())
    .then(data =>{
      dispatch({
        type: PATCH_EVENT,
        payload: eventToUpdate.workers_hired
      })
    })
}

Which receives data from a React event:

  const addWorker = () => {
    const viewedEvent = props.rawEvents.find(event => {
      return event.name === props.event.title
    })

    viewedEvent.workers_hired = 5

    props.patchEvent(viewedEvent)
  }

Where props.patchEvent is my Redux action that is shown in the first code snippet.

When I fire this action I receive a 404 error "Unexpected token < in JSON at position 0"

Now, I understand that this has to do with the fetch response not being in correct JSON but I cannot for the life of me figure out where the formatting has gone wrong in my code. Any help would be greatly appreciated!

Error 404 means that the url you requested not found. Check if you fetch the correct url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM