【问题标题】:ASMX Web method called onceASMX Web 方法调用一次
【发布时间】:2021-11-30 23:27:33
【问题描述】:

我在 asmx 页面中有一个 web 方法。我从 jquery Ajax 调用它。 只需 1 次即可正常工作,然后永远不会调用 web 方法。

      [WebMethod(EnableSession = true)]
  public static string WsCom(AjaxObjectsHelper.UpdatePageAjax Param)
  {
     try
     {
        if (HttpContext.Current.Session["AppName"] != null && HttpContext.Current.Session["USR"] != null)
        {
           string xRes = "";
           if (Param.Action == "_GMH")
           {
              xRes = "";
           }
           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));
     }
  }

这里是我的 ajax 调用:

function (xF, xCB) {
    var callData = { Param: { Action: "_GMH", Data: xF } }        
    $.ajax({
        type: "POST",
        url: "/mypage.asmx/WsCom" 
        data: JSON.stringify(callData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function (response) {
    console.log("success")
        }
    }).fail(function (jqXHR, textStatus, error) {
        console.log(jqXHR.responseText);
    });
}

每次调用ajax成功,而不是asp.net webmethod。 (它只是第一次断点) 任何想法?好像是缓存问题。

【问题讨论】:

  • 可能是浏览器正在缓存数据。如果第一个响应,您可以检查过期标头吗?
  • 第一次(当它工作时)缓存控制:无缓存过期:-1 调用缓存控制:私有,max-age=0 过期未显示

标签: jquery asp.net ajax


【解决方案1】:

要允许使用 ASP.NET AJAX 从脚本调用 Web 服务,请在命名空间列表之后添加以下行。

[System.Web.Script.Services.ScriptService]

另外,将以下行添加到您的方法中。

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string WsCom(AjaxObjectsHelper.UpdatePageAjax Param)
{

【讨论】:

    猜你喜欢
    • 2011-04-05
    • 1970-01-01
    • 2013-08-24
    • 2013-04-04
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多