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

ASMX的WebMethod仅首次被jQuery Ajax触发后续调用无效问题求助

问题原因

  1. 前端AJAX代码存在语法错误,url配置项后缺失逗号,可能导致请求参数序列化异常触发隐性缓存。
  2. 虽然POST请求标准上不会被缓存,但旧版IE、部分代理服务、CDN会缓存参数完全相同的POST响应,jQuery自带的cache: false仅对GET请求生效,对POST请求无作用。
  3. 服务端ASMX开启了输出缓存,或者WebMethod配置了缓存特性,导致相同请求直接返回缓存结果不进入业务逻辑。
  4. 首次请求后Session被清空,方法直接进入未授权分支返回结果,被误判为没有进入WebMethod。

解决方法

1. 修正前端AJAX代码

function (xF, xCB) {
    var callData = { Param: { Action: "_GMH", Data: xF } }        
    $.ajax({
        type: "POST",
        // 修正缺失的逗号,同时添加时间戳参数强制请求不被缓存
        url: "/mypage.asmx/WsCom?_t=" + new Date().getTime(),
        data: JSON.stringify(callData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        // 新增请求头强制禁止缓存
        headers: {
            "Cache-Control": "no-cache, no-store, must-revalidate",
            "Pragma": "no-cache",
            "Expires": "0"
        },
        success: function (response) {
            console.log("success", response)
        }
    }).fail(function (jqXHR, textStatus, error) {
        console.log(jqXHR.responseText);
    });
}

2. 服务端禁用WebMethod缓存

给目标方法添加禁用缓存的特性,同时可新增调试日志确认请求是否进入方法:

[WebMethod(EnableSession = true)]
// 禁用响应缓冲
[System.Web.Services.WebMethod(BufferResponse = false)]
// 完全禁用输出缓存
[System.Web.UI.OutputCache(Location = System.Web.UI.OutputCacheLocation.None, NoStore = true, Duration = 0)]
public static string WsCom(AjaxObjectsHelper.UpdatePageAjax Param)
{
    // 新增调试日志,确认方法是否被调用,避免断点异常导致的误判
    System.Diagnostics.Debug.WriteLine("WsCom调用时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
    try
    {
        if (HttpContext.Current.Session["AppName"] != null && HttpContext.Current.Session["USR"] != null)
        {
            string xRes = "";
            if (Param.Action == "_GMH")
            {
                // 测试阶段先返回非空值,避免空响应被判定为缓存
                xRes = "{\"status\":\"ok\"}";
            }
            else if (Param.Action == "_GMHL")
            {
                xRes = "";
            }
            else
            {
                xRes = SiteHelper.AjaxObjectsHelper.GetJson(new SiteHelper.AjaxObjectsHelper.ErrorAjax("Invalid Action.", false));
            }
            return xRes;
        }
        else
        {
            return SiteHelper.AjaxObjectsHelper.GetJson(new SiteHelper.AjaxObjectsHelper.ErrorAjax("Unauthorized access", false));
        }
    }
    catch (Exception ex)
    {
        return SiteHelper.AjaxObjectsHelper.GetJson(new SiteHelper.AjaxObjectsHelper.ErrorAjax(ex.Message, false));
    }
}

3. 检查Web.config缓存配置

确认项目Web.config中没有为ASMX路径配置全局输出缓存,若有以下类似配置需修改或删除:

<location path="mypage.asmx">
  <system.web>
    <caching>
      <!-- 禁用ASMX路径的输出缓存 -->
      <outputCache enableOutputCache="false" />
    </caching>
  </system.web>
</location>

4. 排查验证逻辑

如果调试日志显示方法已经被调用但未进入业务分支,检查首次请求后Session["AppName"]和Session["USR"]是否被清空,调整Session存储逻辑即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 23:54:05