2017-12-13 12 views
0

나는 내 가게에 스트라이프 체크 아웃을 구현하기 위해 노력하고 내가 말하는 오류를 얻을 : 여기스트라이프 체크 아웃 오류

enter image description here 내 코드입니다 :

으로 개체를 구문 분석 오류가처럼
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}`); 
    }); 
    }); 
} 

답변

0

이 보이는 json. 당신이 토큰으로 무엇을 부르고 있는지 아는 것이 도움이 될 것입니다.

요청을 할 때 반드시 application/jsonContent-TypeAccept 헤더를 설정합니다 :

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

항상 오류를 처리하는 catch 블록을 추가해야합니다. 또한 나는 당신이 같은 블럭에서 바로 처리하는 대신에 response.json()을 반환 할 것을 제안한다. (콜백 지옥을 완화하는데 도움이되지 않는 반 패턴이다.)

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); 
    });