Fix AJAX job actions error handling

- Correct AJAX request detection by checking X-Requested-With header only
- Prevent fetch from following redirects with redirect: 'manual'
- Add robust response handling for redirects and JSON responses
- Ensure AJAX requests return JSON instead of HTML redirects
parent 2150cc62
...@@ -222,15 +222,24 @@ ...@@ -222,15 +222,24 @@
headers: { headers: {
'X-Requested-With': 'XMLHttpRequest' 'X-Requested-With': 'XMLHttpRequest'
}, },
body: formData body: formData,
redirect: 'manual' // Don't follow redirects
}) })
.then(response => response.json()) .then(response => {
.then(data => { if (response.status >= 200 && response.status < 300) {
if (data.success) { // Success status
// Show success notification return response.json().then(data => {
if (data.success !== false) {
showActionNotification(jobId, action, 'success');
} else {
showActionNotification(jobId, action, 'error');
}
});
} else if (response.status >= 300 && response.status < 400) {
// Redirect - treat as success for our purposes
showActionNotification(jobId, action, 'success'); showActionNotification(jobId, action, 'success');
// Status will be updated by the polling mechanism
} else { } else {
// Error status
showActionNotification(jobId, action, 'error'); showActionNotification(jobId, action, 'error');
} }
}) })
......
...@@ -403,12 +403,12 @@ def delete_job(job_id): ...@@ -403,12 +403,12 @@ def delete_job(job_id):
if queue_manager.delete_job(job_id, user['id']): if queue_manager.delete_job(job_id, user['id']):
# Check if this is an AJAX request # Check if this is an AJAX request
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded' and request.headers.get('X-Requested-With') == 'XMLHttpRequest': if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return {'success': True, 'message': 'Job deleted successfully!'} return {'success': True, 'message': 'Job deleted successfully!'}
flash('Job deleted successfully!', 'success') flash('Job deleted successfully!', 'success')
else: else:
# Check if this is an AJAX request # Check if this is an AJAX request
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded' and request.headers.get('X-Requested-With') == 'XMLHttpRequest': if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return {'success': False, 'message': 'Failed to delete job or access denied.'}, 400 return {'success': False, 'message': 'Failed to delete job or access denied.'}, 400
flash('Failed to delete job or access denied.', 'error') flash('Failed to delete job or access denied.', 'error')
...@@ -424,12 +424,12 @@ def cancel_job(job_id): ...@@ -424,12 +424,12 @@ def cancel_job(job_id):
if queue_manager.cancel_job(job_id, user['id']): if queue_manager.cancel_job(job_id, user['id']):
# Check if this is an AJAX request # Check if this is an AJAX request
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded' and request.headers.get('X-Requested-With') == 'XMLHttpRequest': if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return {'success': True, 'message': 'Job cancelled successfully!'} return {'success': True, 'message': 'Job cancelled successfully!'}
flash('Job cancelled successfully!', 'success') flash('Job cancelled successfully!', 'success')
else: else:
# Check if this is an AJAX request # Check if this is an AJAX request
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded' and request.headers.get('X-Requested-With') == 'XMLHttpRequest': if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return {'success': False, 'message': 'Failed to cancel job or access denied.'}, 400 return {'success': False, 'message': 'Failed to cancel job or access denied.'}, 400
flash('Failed to cancel job or access denied.', 'error') flash('Failed to cancel job or access denied.', 'error')
...@@ -445,12 +445,12 @@ def restart_job(job_id): ...@@ -445,12 +445,12 @@ def restart_job(job_id):
if queue_manager.restart_job(job_id, user['id']): if queue_manager.restart_job(job_id, user['id']):
# Check if this is an AJAX request # Check if this is an AJAX request
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded' and request.headers.get('X-Requested-With') == 'XMLHttpRequest': if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return {'success': True, 'message': 'Job restarted successfully!'} return {'success': True, 'message': 'Job restarted successfully!'}
flash('Job restarted successfully!', 'success') flash('Job restarted successfully!', 'success')
else: else:
# Check if this is an AJAX request # Check if this is an AJAX request
if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded' and request.headers.get('X-Requested-With') == 'XMLHttpRequest': if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return {'success': False, 'message': 'Failed to restart job or access denied.'}, 400 return {'success': False, 'message': 'Failed to restart job or access denied.'}, 400
flash('Failed to restart job or access denied.', 'error') flash('Failed to restart job or access denied.', 'error')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment