C# ajaxjquery中的成功消息

C# ajaxjquery中的成功消息,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,我正在asp.net中开发一个应用程序,其中我的asp页面中有jquery代码 var strdata = { problemID: $("#problemID").val(), commentText: $("#_1").val(), empID: $("#empID").val(), agree: 0, disagree: 0 }; $.ajax({

我正在asp.net中开发一个应用程序,其中我的asp页面中有jquery代码

var strdata = {
           problemID: $("#problemID").val(),
           commentText: $("#_1").val(),
           empID: $("#empID").val(),
           agree: 0,
           disagree: 0
        };
        $.ajax({
            type: "POST",
            url: "<%= Url.Action("PostComment", "Discussion")  %>",
            data: strdata,
            dataType: "JSON",
            success: function (msg) {
                if ( msg == 1)
                alert("Success" + msg );
            }
        });
模型有代码

public bool postComment(int problemID, string commentText, int empID, int agree, int disagree)
        {
            bool result = false;
            Comment c = new Comment();
            c.ProblemID = problemID;
            c.CommentText = commentText;
            c.EmpID = empID;
            c.Agree = agree;
            c.DisAgree = disagree;

            _objectModel.Comments.InsertOnSubmit(c);
            try
            {
                _objectModel.SubmitChanges();
                result = true;

            }

            catch (Exception ex)
            {
            }


            return result;  

}

数据通过ajax和jquery保存在数据库中,但没有显示成功消息

我认为在“msg”中返回的结果不是数字1。可能msg==true?

要获得AJAX请求的结果,请在成功处理程序中使用以下代码:

success: function (msg) {
            if ( msg.d === true) {
              alert("Success" + msg.d );
            }
         }
您必须通过“d”属性访问结果

要获取错误消息,请使用以下错误处理程序:

error: function(jqXHR, textStatus, errorThrown){
         alert(errorThrown);
       }

如果警报运行时不带或不带表示返回的数据类型不是$.ajax函数所期望的数据类型的条件

有两种方法可以找到问题的根源:

  • 首先打开chrome或firebug,查看网络流量。如果您要返回结果(请求正在发出,内容看起来很准确),那么您的数据类型肯定是原因。尝试更改请求中的数据类型

  • 接下来,您可以尝试添加功能,而不仅仅是成功。还有错误、状态码(404500等)、发送前等检查单据


还有其他方法:)。Fiddler可能也有帮助。

我在没有此条件的情况下运行了此代码,但未显示警报。@Snake:您是否尝试在jquery AJAX调用中使用错误处理程序?您得到了什么错误代码/错误消息?您的数据类型是“JSON”,但我没有看到您输出JSON。尝试将返回语句包装为:var jsonSerializer=new System.Web.Script.Serialization.JavaScriptSerializer();字符串json=jsonSerializer.Serialize(yourCustomObject);更多信息:您可能还需要将页面类型的标题设置为“application/json”。。。也许先不要使用json?
error: function(jqXHR, textStatus, errorThrown){
         alert(errorThrown);
       }