Home > Software design >  ASMX Web method called once
ASMX Web method called once

Time:10-16

i have a web method in asmx page. I call it from jquery Ajax. All work fine just 1 time, next the web method is never called.

      [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));
     }
  }

Here my ajax call:

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);
    });
}

The ajax success is called everytime, but not the asp.net webmethod. (it breakpoint just the first time) Any idea? it look like a cache problem.

CodePudding user response:

To allow Web Service to be called from script, using ASP.NET AJAX, add the following line after namespace list.

[System.Web.Script.Services.ScriptService]

Also, add the following line to your method.

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