Stripe checkout error

202 Views Asked by At

I am trying to implement stripe checkout to me store and I get an error saying: enter image description here

Here is my code:

onToken = (token) => {
  fetch('/save-stripe-token', {
    method: 'POST',
    body: JSON.stringify(token),
  }).then(response => {
    response.json().then(data => {
      alert(`We are in business, ${data.email}`);
    });
  });
}
1

There are 1 best solutions below

0
On

Looks like there was an error parsing the object into json. It would be helpful to know what you are calling onToken with.

Make sure to set Content-Type and Accept headers with application/json when making your request:

fetch('...', {
  // ...
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  // ...
})

Make sure to always add a catch block to deal with errors. Also I suggest you return the response.json() instead of dealing with right away in the same then block (this is an anti-pattern that does not help in alleviating callback hell).

fetch(...)
  .then(response => {
    return response.json();
  })
  .then(data => {
    alert(`We are in business, ${data.email}`);
  })
  .catch(error => {
    // Handle the error here in some way
    console.log(error);
  });