在asp.net中使用ajax jquery插入数据

在asp.net中使用ajax jquery插入数据,jquery,asp.net,ajax,Jquery,Asp.net,Ajax,我不知道是什么问题 <script type="text/javascript"> $(document).ready(function () { $("#SendCommentBtn").click(function () { $.ajax({ type: "POST", contentType: "applica

我不知道是什么问题

        <script type="text/javascript">
        $(document).ready(function () {
            $("#SendCommentBtn").click(function () {
                $.ajax({

                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Pages/PageOne.aspx/SendComment",
                    data: "{'name':'" + $('#nameTxt').val() + "','comment':'" + $('#commentTxt').val() + "'}",
                    async: false,
                    success: function (response) {
                        $("#nameTxt").val("");
                        $("#commentTxt").val("");
                        alert("OK");
                    },
                    error: function () {
                        alert("ERROR");
                    }
                });
            });
        });
</script>

我的问题在哪里?我找不到它。我正在使用母版页。。是否与web表单相同?此代码位于母版页中。。如果在我的代码隐藏中放置断点(SendComment方法),web不会停止。这就像是永远不会到达它

试试这个也许能帮你解决问题

var name = $.trim($('#<%=nameTxt.ClientID %>').val());
var comment= $.trim($('#<%=nameTxt.ClientID %>').val());
$.ajax({

                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Pages/PageOne.aspx/SendComment",
                data: "{'name':'" + name + "','comment':'" + comment + "'}",
                async: false,
                success: function (response) {
                    $("#nameTxt").val("");
                    $("#commentTxt").val("");
                    alert("OK");
                },
                error: function () {
                    alert("ERROR");
                }
            });
var name=$.trim($('#').val());
var comment=$.trim($('#').val());
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“Pages/PageOne.aspx/SendComment”,
数据:“{'name':'“+name+”,'comment':'“+comment+”}”,
async:false,
成功:功能(响应){
$(“#nameTxt”).val(“”);
$(“#commentTxt”).val(“”);
警报(“正常”);
},
错误:函数(){
警报(“错误”);
}
});

我已附上示例代码供您参考

  <script type="text/javascript">
    $(document).ready(function () {
        $("#SendCommentBtn").click(function (e) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Pages/PageOne.aspx/SendComment",
                data: "{'name':'" + $('#<%= nameTxt.ClientID %>').val() + "','comment':'" + $('#<%= commentTxt.ClientID %>').val() + "'}",
                //async: false,
                success: function (response) {
                    $('#<%= nameTxt.ClientID %>').val("");
                    $('#<%= commentTxt.ClientID %>').val("");
                    alert("OK");
                },
                error: function () {
                    alert("ERROR");
                }
            });
            e.preventDefault();
        });
    });
</script>

$(文档).ready(函数(){
$(“#SendCommentBtn”)。单击(函数(e){
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“/Pages/PageOne.aspx/SendComment”,
数据:“{'name':'”+$('#').val()+“,'comment':'”+$('#').val()+“}”,
//async:false,
成功:功能(响应){
$('#').val(“”);
$('#').val(“”);
警报(“正常”);
},
错误:函数(){
警报(“错误”);
}
});
e、 预防默认值();
});
});

检查并让我知道反馈

您确定URL吗@Dilish hi,mm问题是我使用了母版页,SendComment方法在母版页的代码后面。。如何从webforms调用此方法?能否将
SendComment
webmethod移动到“PageOne.aspx”页面本身?另外,如果您使用的是母版页,那么您可以使用$('#'),其中controID是元素的id,通过jquery获取控件。我认为这行不通。看这里
  <script type="text/javascript">
    $(document).ready(function () {
        $("#SendCommentBtn").click(function (e) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Pages/PageOne.aspx/SendComment",
                data: "{'name':'" + $('#<%= nameTxt.ClientID %>').val() + "','comment':'" + $('#<%= commentTxt.ClientID %>').val() + "'}",
                //async: false,
                success: function (response) {
                    $('#<%= nameTxt.ClientID %>').val("");
                    $('#<%= commentTxt.ClientID %>').val("");
                    alert("OK");
                },
                error: function () {
                    alert("ERROR");
                }
            });
            e.preventDefault();
        });
    });
</script>