@@ -67,15 +67,26 @@ function App() {
6767 }
6868
6969 if ( response . ok ) {
70- const data = await response . json ( )
70+ let data : { email ?: string } = { }
71+ try {
72+ data = await response . json ( )
73+ } catch {
74+ setError ( '伺服器回應格式錯誤' )
75+ return
76+ }
7177 if ( data . email ) {
7278 setEmail ( data . email )
7379 setTokenExpired ( false )
7480 } else {
7581 setError ( '無法取得註冊資訊' )
7682 }
7783 } else {
78- const errorData = await response . json ( )
84+ let errorData : { error ?: string } = { }
85+ try {
86+ errorData = await response . json ( )
87+ } catch {
88+ // 回應不是 JSON 格式,忽略解析錯誤
89+ }
7990 // 檢查是否為 token 不存在或已過期的錯誤
8091 if ( response . status === 404 && errorData . error === 'token 不存在或已過期' ) {
8192 setTokenExpired ( true )
@@ -172,7 +183,13 @@ function App() {
172183 `${ API_BASE_URL } /register/result?transactionId=${ transactionId } `
173184 )
174185 if ( response . ok ) {
175- const data : RegistrationResult = await response . json ( )
186+ let data : RegistrationResult
187+ try {
188+ data = await response . json ( )
189+ } catch {
190+ console . error ( '檢查註冊狀態失敗: 伺服器回應格式錯誤' )
191+ return
192+ }
176193 // 如果是 "Waiting for registration" 訊息,繼續輪詢
177194 if ( data . message === 'Waiting for registration' ) {
178195 console . log ( '等待註冊:' , data . message )
@@ -187,10 +204,20 @@ function App() {
187204 }
188205 } else if ( response . status === 400 ) {
189206 // 還在等待驗證,繼續輪詢
190- const data = await response . json ( )
207+ let data : { message ?: string } = { }
208+ try {
209+ data = await response . json ( )
210+ } catch {
211+ // 回應不是 JSON 格式,忽略
212+ }
191213 console . log ( '等待註冊:' , data . message )
192214 } else {
193- const errorData = await response . json ( )
215+ let errorData : { error ?: string } = { }
216+ try {
217+ errorData = await response . json ( )
218+ } catch {
219+ // 回應不是 JSON 格式,忽略
220+ }
194221 console . error ( '檢查註冊狀態失敗:' , errorData )
195222 }
196223 } catch ( err ) {
0 commit comments