jquery ajax调用在asp.net中运行C#代码隐藏方法

jquery ajax调用在asp.net中运行C#代码隐藏方法,jquery,ajax,Jquery,Ajax,我需要使用ajax方法从html javascript调用ASP.NET C#代码隐藏方法 jQuery ajax调用没有响应。它不显示任何内容,没有错误消息或任何东西。我还尝试了jQueryAPI的CDN url引用 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="jquery-1.12

我需要使用ajax方法从html javascript调用ASP.NET C#代码隐藏方法

jQuery ajax调用没有响应。它不显示任何内容,没有错误消息或任何东西。我还尝试了jQueryAPI的CDN url引用

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script src="jquery-1.12.4.min.js" type="text/javascript"></script>
</head>
<body>




    <script type="text/javascript">

        $(document).ready(function () {

            $('#btn1').click(function (e) {
                $.ajax({
                    type: "POST",
                    url: 'WebForm2.aspx/TestMethod',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        alert('Success : ' + data.d);
                    },
                    failure: function (AjaxResponse) {
                        alert('Failed: ' + AjaxResponse.d);
                    }
                });
            });

        });

    </script>

        <input id="btn1" type="button" value="Show Method Output" />
</body>
</html>

它应该显示“代码隐藏方法的结果”。如果有人告诉我哪里出了错,我将不胜感激,谢谢

尝试更改数据类型:“text”,或者干脆去掉它,让jquery来解决它。现在您已经将其设置为json,但是您正在返回文本控制台日志中有什么值得注意的吗?
namespace AjaxCallProject
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        [System.Web.Services.WebMethod()]
        public static string TestMethod()
        {
             return "Result from code behind method";
        }

    }
}