Fix delete JSON error: API routes return 401 JSON instead of HTML redirect, robust fetch error handling
This commit is contained in:
parent
58fcddbff1
commit
7844e82c95
2 changed files with 12 additions and 1 deletions
3
app.py
3
app.py
|
|
@ -438,6 +438,9 @@ def login_required(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated_function(*args, **kwargs):
|
def decorated_function(*args, **kwargs):
|
||||||
if not session.get('authenticated'):
|
if not session.get('authenticated'):
|
||||||
|
# API-Routen (/api/...) geben JSON zurück statt HTML-Redirect
|
||||||
|
if request.path.startswith('/api/'):
|
||||||
|
return jsonify({'success': False, 'error': 'not_authenticated'}), 401
|
||||||
return redirect(url_for('login'))
|
return redirect(url_for('login'))
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
|
||||||
|
|
@ -238,8 +238,16 @@ document.addEventListener('click', function(e) {
|
||||||
const id = btn.dataset.id;
|
const id = btn.dataset.id;
|
||||||
if (!confirm('Eintrag #' + id + ' aus dem Log löschen?')) return;
|
if (!confirm('Eintrag #' + id + ' aus dem Log löschen?')) return;
|
||||||
fetch('/api/sent-emails/' + id + '/delete', {method: 'POST', credentials: 'same-origin'})
|
fetch('/api/sent-emails/' + id + '/delete', {method: 'POST', credentials: 'same-origin'})
|
||||||
.then(r => { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); })
|
.then(r => {
|
||||||
|
if (r.redirected || r.status === 302 || r.status === 401) {
|
||||||
|
window.location.reload(); return null;
|
||||||
|
}
|
||||||
|
const ct = r.headers.get('content-type') || '';
|
||||||
|
if (!ct.includes('application/json')) throw new Error('Unerwartete Antwort (HTTP ' + r.status + ')');
|
||||||
|
return r.json();
|
||||||
|
})
|
||||||
.then(d => {
|
.then(d => {
|
||||||
|
if (!d) return;
|
||||||
if (d.success) {
|
if (d.success) {
|
||||||
const row = document.getElementById('sent-row-' + id);
|
const row = document.getElementById('sent-row-' + id);
|
||||||
if (row) row.remove();
|
if (row) row.remove();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue