Skip to content

Commit fb27d71

Browse files
authored
Update index.html
1 parent e623d59 commit fb27d71

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

simulators/roller-coaster/index.html

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,7 @@ <h1>RCS &middot; Physics</h1>
470470

471471
<script>
472472
// =====================================================
473-
// COMPLETE FRONTEND CODE - ALL BUTTONS AND LOGIC INCLUDED
474-
// All calculations call the backend API
473+
// COMPLETE FRONTEND CODE - FIXED API URL AND ERROR HANDLING
475474
// =====================================================
476475

477476
// ═══════════════════════════════════════════════════════════════
@@ -482,7 +481,7 @@ <h1>RCS &middot; Physics</h1>
482481
const G_CAUTION = 5.0, G_WARNING = 6.5, G_NEG_WARN = 2.0;
483482
let UNITS = 'SI'; // 'SI' | 'IMP'
484483

485-
// API Configuration - REPLACE THIS WITH YOUR ACTUAL VERCEL URL
484+
// ⚡ FIXED API URL - Uses your actual Vercel domain from the screenshot ⚡
486485
const API_BASE_URL = 'https://aion-backend-mu.vercel.app/api/simulators/roller-coaster';
487486

488487
// User-friendly error messages
@@ -614,24 +613,42 @@ <h1>RCS &middot; Physics</h1>
614613
).join('')}${extra}</div>`;
615614
}
616615

617-
// ═══════════════════════════════════════════════════════════════
618-
// API CALL FUNCTION
619-
// ═══════════════════════════════════════════════════════════════
616+
// =====================================================
617+
// FIXED API CALL FUNCTION - Handles 404 and non-JSON responses
618+
// =====================================================
620619
async function callSimulator(module, params) {
621-
const response = await fetch(API_BASE_URL, {
622-
method: 'POST',
623-
headers: { 'Content-Type': 'application/json' },
624-
body: JSON.stringify({ module, params, unitSystem: UNITS })
625-
});
626-
627-
const data = await response.json();
628-
629-
if (!response.ok || data.error) {
630-
const userMessage = ERROR_MESSAGES[data.error] || data.userMessage || 'Calculation error';
631-
throw { userMessage, ...data };
620+
try {
621+
const response = await fetch(API_BASE_URL, {
622+
method: 'POST',
623+
headers: { 'Content-Type': 'application/json' },
624+
body: JSON.stringify({ module, params, unitSystem: UNITS })
625+
});
626+
627+
// Check if the response is JSON
628+
const contentType = response.headers.get('content-type');
629+
if (!contentType || !contentType.includes('application/json')) {
630+
// If it's not JSON, it's probably a 404 HTML page
631+
const text = await response.text();
632+
console.error('Non-JSON response:', text.substring(0, 200));
633+
throw {
634+
userMessage: 'Backend connection failed. The API endpoint might be wrong or the server is down.',
635+
details: `Status: ${response.status}`
636+
};
637+
}
638+
639+
const data = await response.json();
640+
641+
if (!response.ok || data.error) {
642+
const userMessage = ERROR_MESSAGES[data.error] || data.userMessage || 'Calculation error';
643+
throw { userMessage, ...data };
644+
}
645+
646+
return data;
647+
648+
} catch (error) {
649+
console.error('API call failed:', error);
650+
throw error;
632651
}
633-
634-
return data;
635652
}
636653

637654
// ═══════════════════════════════════════════════════════════════
@@ -689,7 +706,7 @@ <h1>RCS &middot; Physics</h1>
689706
} catch (error) {
690707
document.getElementById('ec-result').innerHTML =
691708
`<div class="result-box" style="border-left-color:var(--red)">
692-
<div style="color:var(--red);font-family:var(--mono)">${error.userMessage || 'Connection error'}</div>
709+
<div style="color:var(--red);font-family:var(--mono)">${error.userMessage || 'Connection error. Check console.'}</div>
693710
</div>`;
694711
}
695712
});

0 commit comments

Comments
 (0)