[英]Receiving error Unexpected token < in JSON at position 0; is this a backend or frontend problem?
I currently have a PATCH method:我目前有一个 PATCH 方法:
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:从 React 事件接收数据:
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.其中 props.patchEvent 是第一个代码片段中显示的 Redux 操作。
When I fire this action I receive a 404 error "Unexpected token < in JSON at position 0"当我触发此操作时,我收到 404 错误“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.现在,我知道这与获取响应不正确有关 JSON 但我一生都无法弄清楚我的代码中的格式哪里出错了。 Any help would be greatly appreciated!任何帮助将不胜感激!
Error 404 means that the url you requested not found.错误 404 表示未找到您请求的 url。 Check if you fetch the correct url.检查您是否获取正确的 url。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.