Handle response - SyntaxError: Unexpected end of input when using mode: 'no-cors'

前端 未结 5 1860
南旧
南旧 2020-11-22 07:19

I tried a ReactJS fetch call to a REST-API and want to handle the response. The call works, i get a response, which i can see in Chrome Dev Tools:

function g         


        
5条回答
  •  日久生厌
    2020-11-22 07:48

    I know this answer might be super late and might have been resolved but i just had the same issue today and I just needed to add a ',' at the end of the headers hash and i stopped getting the error

    export function addContacts(formData) {
      return(dispatch) => {
        dispatch({type: 'POSTING_CONTACTS'});
        console.log(formData)
        return fetch(uri, {
          method: 'POST',
          body: JSON.stringify({contact: {name: formData.name, phone_number: formData.phoneNumber}}),
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json'
          },
        })
        .then(response => {
            return response.json()
          }).then(responseJSON => {
            console.log(responseJSON)
            return dispatch({type: 'ADD_CONTACT', payload: responseJSON});
          })
      }
    }     
    

提交回复
热议问题