2

I am trying to call a c# method using ajax as below.

<a href="#divrecentQ" id="linkdivrecentQ" onclick="lnkClick();" aria-controls="divrecentQ" role="tab" data-toggle="tab">Click here</a>

Here is the method in JS

    function lnkClick() {
        alert("called");
        $.ajax({
            type: "POST",
            url: '/amain.aspx/LoadImages',
            data: {},
            success: function () {
                alert(1);
            },
            dataType: 'html'
        });
        alert("cal");
    }

Server side:

    public static void LoadImages()
    {
       
        log.Debug("LoadImages is called");
       
    }

Server side method is not getting called.

Can someone please help?

Thanks

Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80

2 Answers2

3

You should define the method static and wrap it with [WebMethod] attribute.

[WebMethod]
public static void LoadImages()
{

    Label1.Text = "hi therre";
    Response.Redirect("www.google.com");
    log.Debug("LoadImages is called");

}
mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

Adding "contentType: "application/json; charset=utf-8"," in jquery called the server side method.

Thanks everyone for help. :)

Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80