2

I want to call code behind method from client side ajax request. It returns success but it never goes inside this method. Here is my code:

Code Behind:

        [WebMethod]
        public static void Test()
        {
            var ceva = "I was called";
        }

javascript ajax request:

            $.ajax({
                type: "POST",
                url: "/Default.aspx/Test",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    alert('success');
                },
                error : function(data , data2 , data3) {
                    alert('failed');
                }
            });
7
  • How do you verify it never executes the method? Did you put a Debug.Print statement in there...? Commented Mar 26, 2014 at 7:33
  • Yes I put but it never goes there. Commented Mar 26, 2014 at 7:35
  • Try to add data property to ajax (could be an empty value, like data: {}) and add dataType: "json" Commented Mar 26, 2014 at 7:40
  • Again nothing happened. Commented Mar 26, 2014 at 7:43
  • You checked with firebug or the developer toolbar, in the net section, the call? maybe the content type or something else is wrong in the call Commented Mar 26, 2014 at 7:54

1 Answer 1

5

Please try: (tested and working)

        $.ajax({
            type: "POST",
            url: "Default.aspx/Test",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                alert('success');
            },
            error : function(data) {
                alert('failed');
            }
        });

code:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Test()
{
        return "I was called";
}
Sign up to request clarification or add additional context in comments.

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.