You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2后台Ajax请求返回HTML而非JSON问题求助

Magento 2 后台Ajax请求返回HTML而非JSON导致数据不显示问题

问题说明

将Magento 2代码迁移至新服务器后,出现以下差异:

  • 本地Wamp环境后台(如销售订单页)正常加载数据,对应Ajax请求返回JSON格式数据
  • 新服务器后台页面无数据,对应Ajax请求返回HTML格式的网格加载模板(无实际数据)
  • 新服务器集成了Amazon CloudFront和ThreatX防护服务

本地请求核心参数示例:

http://abc.local/admin_4ajzfg/mui/index/render/key/[密钥]/?namespace=sales_order_grid&search=sand&isAjax=true&[分页/排序参数]

新服务器请求核心参数示例:

https://abc.test/admin_4ajzfg/mui/index/render/key/[密钥]/?namespace=sales_order_grid&search=sandeep&isAjax=true&[分页/排序参数]

新服务器返回的HTML内容:

<!--
/**
 * Copyright &copy; Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<div class="admin__data-grid-outer-wrap" data-bind="scope: 'sales_order_grid.sales_order_grid'">
    <div data-role="spinner" data-component="sales_order_grid.sales_order_grid.sales_order_columns" class="admin__data-grid-loading-mask">
        <div class="spinner">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
    </div>
    <!-- ko template: getTemplate() -->
    <!-- /ko -->
    <script type="text/x-magento-init">
        {"*": {"Magento_Ui/js/core/app": more_code_is_here}}
    </script>
</div>

排查与解决方法

1. 检查ThreatX防护规则

ThreatX的防护逻辑可能误判后台Ajax请求,拦截或篡改了带isAjax=true参数的请求,导致Magento无法识别为Ajax请求,返回HTML模板而非JSON数据。

解决步骤:

  • 在ThreatX控制台将后台管理路径(如/admin_4ajzfg/)加入白名单,允许该路径下的所有请求通过
  • 添加规则放行带有isAjax=true参数的请求
  • 验证ThreatX是否过滤了X-Requested-With请求头(Magento依赖该头判断Ajax请求),确保该头能完整传递到后端

2. 调整CloudFront缓存与转发配置

CloudFront的缓存策略或请求头转发规则错误,可能导致后端无法正确识别请求类型。

解决步骤:

  • 针对/mui/index/render/这类动态接口,配置CloudFront禁用缓存,或设置缓存键包含isAjax、key等所有动态参数
  • 配置CloudFront转发规则,确保X-Requested-With、Content-Type等请求头完整传递到源服务器
  • 手动清空CloudFront缓存,重新测试请求响应

3. 验证服务器端配置

即使代码一致,新服务器的环境配置仍可能存在差异:

  • 对比本地与新服务器的php.ini配置,确保allow_url_fopen、session.save_path等参数一致
  • 检查app/etc/env.php中的数据库连接、缓存配置是否正确
  • 执行Magento命令重置静态资源与缓存:
    bin/magento setup:static-content:deploy -f
    bin/magento cache:clean
    bin/magento cache:flush
    
  • 查看服务器日志(var/log/system.log、var/log/exception.log),排查请求处理过程中的报错信息

4. 确认请求头传递完整性

Magento判断Ajax请求依赖X-Requested-With: XMLHttpRequest请求头,若该头被代理或防护系统过滤,后端会返回HTML而非JSON。

验证方法:

  • 使用curl直接向源服务器发送请求,手动添加X-Requested-With: XMLHttpRequest头,检查是否返回JSON
  • 对比本地与新服务器的请求头,确认所有必要字段未被过滤

内容的提问来源于stack exchange,提问作者Sandeep Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:26:03