模型检测异常:Unexpected token '<', "<!DOCTYPE "... is not valid JSON

2026年05月04日 20:36 状态: processing

🚨 错误信息

模型检测异常:Unexpected token '<', "<!DOCTYPE "... is not valid JSON

🤖 AI解决方案

报错分析报告

1.错误翻译



原文:Unexpected token '<', "译文:意外的字符'<', "

2. 原因分析


  • 根本原因:预期接收JSON数据,但实际收到HTML内容(含声明),JSON解析器无法识别<字符。

  • 发生位置:调用JSON解析方法(如JSON.parse())处理HTTP响应体时,响应内容为HTML而非JSON。
  • 3.解决方案


    ``javascript
    // 修复后的代码示例(以Fetch API为例)
    async function fetchData(url) {
    try {
    const response = await fetch(url);

    // 1. 检查响应状态
    if (!response.ok) throw new Error(
    HTTP error! status: ${response.status});

    // 2. 验证Content-Type是否为JSON
    const contentType = response.headers.get('content-type');
    if (!contentType || !contentType.includes('application/json')) {
    throw new Error(
    Expected JSON, got ${contentType});
    }

    // 3. 安全解析JSON
    const data = await response.json();
    return data;
    } catch (error) {
    console.error('请求失败:', error.message);
    // 处理错误(如返回默认数据/提示用户)
    }
    }
    `

    4. 预防措施


  • 最佳实践1:请求时设置Accept: application/json头,明确要求JSON响应;解析前验证Content-Type

  • 最佳实践2:用try/catch`包裹JSON解析逻辑,捕获并处理解析失败场景。

  • 推荐工具:用浏览器「网络面板」检查响应内容/类型,或用Postman验证接口返回格式。
  • ---
    :若需分析其他报错,请提供具体错误信息。