修复二维码生成器问题
This commit is contained in:
24
js/common.js
24
js/common.js
@@ -27,11 +27,25 @@ async function apiRequest(endpoint, options = {}) {
|
||||
const baseUrl = window.API_BASE || '/api/v1';
|
||||
const url = endpoint.startsWith('http') ? endpoint : `${baseUrl}${endpoint}`;
|
||||
|
||||
// 默认 headers
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers
|
||||
};
|
||||
|
||||
// 如果没有设置 Content-Type 且 body 是 URLSearchParams,不自动添加
|
||||
if (!options.headers || !options.headers['Content-Type']) {
|
||||
if (options.body && options.body instanceof URLSearchParams) {
|
||||
// 表单数据,不添加 JSON Content-Type
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
} else if (options.body && typeof options.body === 'string') {
|
||||
// 已经是字符串,不添加
|
||||
} else if (options.body && typeof options.body === 'object') {
|
||||
// JSON 对象
|
||||
headers['Content-Type'] = 'application/json';
|
||||
options.body = JSON.stringify(options.body);
|
||||
}
|
||||
}
|
||||
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
@@ -51,7 +65,7 @@ async function apiRequest(endpoint, options = {}) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 先获取响应文本,用于调试
|
||||
// 获取响应文本
|
||||
const text = await response.text();
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -65,7 +79,11 @@ async function apiRequest(endpoint, options = {}) {
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
|
||||
return JSON.parse(text);
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (e) {
|
||||
return text;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('API 请求错误:', error);
|
||||
throw error;
|
||||
|
||||
@@ -50,16 +50,14 @@ async function generateQR() {
|
||||
const size = parseInt(document.getElementById('size').value);
|
||||
|
||||
try {
|
||||
// 使用 URLSearchParams 发送表单数据
|
||||
const formData = new URLSearchParams();
|
||||
formData.append('content', content);
|
||||
formData.append('size', size);
|
||||
|
||||
const data = await apiRequest('/qrcode/generate', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: formData
|
||||
body: formData // 不需要设置 headers,apiRequest 会自动处理
|
||||
});
|
||||
|
||||
currentQRCode = data.qr_code;
|
||||
|
||||
Reference in New Issue
Block a user