matthew-andrews / isomorphic-fetch

Isomorphic WHATWG Fetch API, for Node & Browserify
MIT License
6.95k stars 289 forks source link

Cannot set header in SPA? #122

Closed nghiepdev closed 6 years ago

nghiepdev commented 7 years ago

Hi all, I have a code example:

fetch(URL, {
    'GET',
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json',
      'Foobaz': 1234567
    },
  })

With app SPA, I cannot get my header.

Please help, thanks!!!!!

error

lmj0011 commented 7 years ago

maybe try making the Foobaz value a string

also change your method to POST if you're sending a body GET passes data in the form of a query string (eg. foo=2&bar=2)

fetch(URL, {
    'GET', // <--- change this to POST
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json',
      'Foobaz': 1234567  // <---  change 1234567 to '1234567'
    },
  })
olieidel commented 7 years ago

Have you set mode to no-cors (for development) by any chance? In that case, it's not possible to set the Content-Type header, as seen here.

Just tripped over this myself and lost 2 hours, hope it helps.

itsazzad commented 7 years ago

@tronghiep92 How did you solve that?

nghiepdev commented 6 years ago

@itsazzad I've switched to axios 😄