vendredi 29 mai 2015

Codebehind function not being called by Ajax POST

My Ajax post is not running my code behind method and thus not returning any data.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="AspTest.Test" %>

Test.aspx (Ajax script)

    <script>
                $(".avatarThumb").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "Test.aspx/MyMethod",
                        //data: {"s": "Some data passed through" },
                        //contentType: 'application/json; charset=utf-8',
                        //dataType: 'json',
                        success: function (response) {
                            alert(response.d); //Returns "undefined"
                        },
                        failure: function(response) {
                            alert(response);
                        }
                    });
                });
            </script>

Removing contentType and dataType does reach success but does not run the codeBehind method. With contentType and/or dataType active it will not reach success nor failure. FF firebug does not display any errors.

*Test.aspx.cs CodeBehind method

        [System.Web.Services.WebMethod]
        public static string MyMethod()
        {
(*)         Debug.WriteLine("MyMethod called!"); // (*) Breakpoint never reached

            return "Method called!";
        }

Aucun commentaire:

Enregistrer un commentaire