Fix dynamic API base path for server deployment

This commit is contained in:
pongpon pitisuk
2026-09-12 14:34:09 +07:00
parent 72eb7cd0c8
commit e01d01464a
9 changed files with 262 additions and 52 deletions
+2 -1
View File
@@ -798,7 +798,8 @@ async function changeStatus(id, status) {
async function generateLink(id) {
try {
const data = await API.post('surveys.php?action=generateLink', { id });
const fullUrl = data.full_url || `${window.location.origin}/survey/respond.php?link=${data.unique_link}`;
const basePath = window.location.pathname.includes('/survey') ? '/survey' : '';
const fullUrl = data.full_url || `${window.location.origin}${basePath}/respond.php?link=${data.unique_link}`;
// Find survey title
const survey = surveysList.find(s => s.id == id);
+6 -2
View File
@@ -3,8 +3,12 @@
* API communication, Auth state, Toast notifications, Theme
*/
const getAppBasePath = () => {
return window.location.pathname.includes('/survey') ? '/survey' : '';
};
const API = {
base: '/survey/api',
base: `${getAppBasePath()}/api`,
async request(endpoint, options = {}) {
const url = `${this.base}/${endpoint}`;
@@ -66,7 +70,7 @@ const Auth = {
async logout() {
await API.post('auth.php?action=logout', {});
this.user = null;
window.location.href = '/survey/index.html';
window.location.href = `${getAppBasePath()}/index.html`;
},
isAdmin() { return this.user?.role === 'admin'; },
+4 -3
View File
@@ -76,7 +76,7 @@ function renderEditor() {
<input type="hidden" id="edit-survey-cert-template-path" value="${escapeHtml(currentSurvey.certificate_template || '')}">
<div id="cert-preview-wrapper" style="margin-top:12px; display:${currentSurvey.certificate_template ? 'block' : 'none'};">
<img id="cert-template-preview" src="${currentSurvey.certificate_template ? '/survey/' + currentSurvey.certificate_template : ''}" style="max-height:140px; width:auto; border-radius:8px; border:1px solid var(--border-color); box-shadow:var(--shadow-sm);">
<img id="cert-template-preview" src="${currentSurvey.certificate_template ? (window.location.pathname.includes('/survey') ? '/survey/' : '/') + currentSurvey.certificate_template : ''}" style="max-height:140px; width:auto; border-radius:8px; border:1px solid var(--border-color); box-shadow:var(--shadow-sm);">
</div>
</div>
@@ -486,7 +486,8 @@ async function uploadCertTemplate(input) {
formData.append('template_image', file);
try {
const res = await fetch('/survey/api/surveys.php?action=uploadTemplate', {
const basePath = window.location.pathname.includes('/survey') ? '/survey' : '';
const res = await fetch(`${basePath}/api/surveys.php?action=uploadTemplate`, {
method: 'POST',
body: formData
});
@@ -501,7 +502,7 @@ async function uploadCertTemplate(input) {
// Update preview
const previewImg = document.getElementById('cert-template-preview');
const previewWrapper = document.getElementById('cert-preview-wrapper');
previewImg.src = '/survey/' + data.template_path;
previewImg.src = (basePath ? `${basePath}/` : '/') + data.template_path;
previewWrapper.style.display = 'block';
// Add dynamic delete button if it doesn't exist
+4 -2
View File
@@ -15,7 +15,8 @@ async function initSurvey() {
}
try {
const data = await fetch(`/survey/api/surveys.php?action=getByLink&link=${encodeURIComponent(link)}`).then(r => r.json());
const basePath = window.location.pathname.includes('/survey') ? '/survey' : '';
const data = await fetch(`${basePath}/api/surveys.php?action=getByLink&link=${encodeURIComponent(link)}`).then(r => r.json());
if (data.error) {
showError(data.error);
@@ -249,7 +250,8 @@ async function submitSurveyResponse(e) {
const params = new URLSearchParams(window.location.search);
try {
const res = await fetch('/survey/api/responses.php?action=submit', {
const basePath = window.location.pathname.includes('/survey') ? '/survey' : '';
const res = await fetch(`${basePath}/api/responses.php?action=submit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
+2 -1
View File
@@ -374,7 +374,8 @@ async function teacherDuplicateSurvey(id) {
async function teacherGenerateLink(id) {
try {
const data = await API.post('surveys.php?action=generateLink', { id });
const fullUrl = data.full_url || `${window.location.origin}/survey/respond.php?link=${data.unique_link}`;
const basePath = window.location.pathname.includes('/survey') ? '/survey' : '';
const fullUrl = data.full_url || `${window.location.origin}${basePath}/respond.php?link=${data.unique_link}`;
// Find survey title
const survey = teacherSurveys.find(s => s.id == id);