Search code examples
javascriptc#asp.netasmxwebmethod

asp.net Web Method not working


I trying call webmethod in my web project. I didnt found a problem in my codes and I called webmethod thousands of times before but I never see mistake like this before. Never enter my methods codes and it return my html page codes to me. ( sorry my bad english :)).. Please someone help me about that.

My Web method codes :

[WebMethod]
    public static string GirisKontrol(string UserName, string Pass)
    {
        try
        {
            string strSonuc = "";
            var context = new DBEntities();
            var Kisi = context.users.Where(t => t.eposta== UserName  && t.sifre == Pass).FirstOrDefault();
            if (Kisi != null && Kisi.uye_onay==1)
            {
                HttpContext.Current.Session["UyeID"] = Kisi.ID;
                HttpContext.Current.Session["Gorev"] = Kisi.gorev;


                return Kisi.adsoyad;
            }
            else
                return "0";
        }
        catch (Exception ex)
        {
            return "";
        }
    }

My javascript codes:

 function GirisKontrol() {
            PageMethods.GirisKontrol("asd", "sad", function(a) {
                alert(a);
});
}

or I tried this but I got same result:

function test() {
        $.ajax({
            type: "POST",
            url: "indexDeneme.aspx/GirisKontrol",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg);
            }
        });
    }

This is result and it never go in c# codes: enter image description here


Solution

  • Try this:

    [WebMethod(EnableSession=true)]
    

    Otherwise your method won't have access to the current SessionState, and the part where you try to save data to Session won't work.

    The first example where you call PageMethods.GirisKontrol is unclear - your JavaScript isn't going to know about the server-side methods.

    The second example using Ajax looks closer, but you're not passing any data.

    data: "{}",
    

    perhaps you mean this:

    data: "{'UserName': 'asd', 'Pass': 'sad'}",