首页 > 解决方案 > ASMX Web 方法调用一次

问题描述

我在 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。(它只是第一次断点)有什么想法吗?它看起来像一个缓存问题。

标签: jqueryasp.netajax

解决方案


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

[System.Web.Script.Services.ScriptService]

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

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

推荐阅读