fix: Add output buffer cleansing in Response::json and enhance error logging for conversation parsing
This commit is contained in:
@@ -54,7 +54,11 @@ class Response
|
||||
$this->sendHeaders();
|
||||
http_response_code($this->statusCode);
|
||||
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||||
if (ob_get_length()) {
|
||||
ob_clean();
|
||||
}
|
||||
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
@@ -668,21 +668,33 @@ class TeacherPortal
|
||||
const res = await fetch('/api/chat/conversations', {
|
||||
headers: { 'Authorization': 'Bearer ' + token }
|
||||
});
|
||||
const data = await res.json();
|
||||
const text = await res.text();
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch (jsonErr) {
|
||||
console.error('Conversations parse error:', jsonErr, 'Raw:', text);
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (container) {
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">خطأ في معالجة البيانات</div>';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.ok && data.status === 'success') {
|
||||
renderConversations(data.data);
|
||||
renderConversations(data.data || []);
|
||||
} else {
|
||||
console.warn('Load convs returned:', data);
|
||||
console.warn('Load convs returned error payload:', data);
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (container) {
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px;">' + (data.message || 'لا توجد محادثات نشطة') + '</div>';
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Load convs error:', e);
|
||||
console.error('Load convs network error:', e);
|
||||
const container = document.getElementById('conv_list_container');
|
||||
if (container) {
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">تعذر تحميل المحادثات (خطأ في الاتصال)</div>';
|
||||
container.innerHTML = '<div style="padding: 20px; text-align: center; color: #F87171; font-size: 12px;">تعذر تحميل المحادثات</div>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user