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

10
  • don't you need to specify [WebMethod] on the method? Commented Nov 7, 2016 at 14:11
  • made the changes. still not working Commented Nov 7, 2016 at 14:14
  • error: function (xhr, status, error) { console.log(error); } try this code inside ajax to check any error Commented Nov 7, 2016 at 14:23
  • 1
    Please refer this : stackoverflow.com/questions/23033614/…. If you have routeConfig file under App_Start, you need to change the AutoRedirectMode Commented Nov 7, 2016 at 14:37
  • 1
    Add error function for ajax so you will see the error message in your console. Commented Nov 7, 2016 at 14:43

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

6 Comments

apology. did undo.
@DKR put breaking point in load images check if it is going in the method. IF it is not check your console for errors from javascript side
it is not going in method. How do I start console to chek js errors?
@DKR in chrome right mouse click-> Inspect-> Second tab -> Console. Clear the console try to execute your ajax check for errors.
It gives error as "undefined". but not about which symbol
|
0

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

Thanks everyone for help. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.