我開發在asp.net應用程序中,我有jQuery代碼在我的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(string problemID, string commentText, string empID, string agree, string disagree)
{
return _discussionRepository.postComment(Convert.ToInt32(problemID), commentText, Convert.ToInt32(empID), Convert.ToInt32(agree),Convert.ToInt32(disagree));
}
和模型代碼
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節省,但成功的消息沒有顯示
我得到了一個錯誤? [目標對象] – Snake
你的數據類型的錯誤是「JSON」,但我沒有看到你輸出JSON嘗試用你的包裹return語句:VAR jsonSerializer =新System.Web.Script.Serialization.JavaScriptSerializer(); JSON字符串= jsonSerializer.Serialize(yourCustomObject); 更多的信息在這裏:http://stackoverflow.com/questions/2287399/encode-object-to-json 此外,您可能需要設置頁面類型的標題爲「應用程序/ json「...嘗試先不使用json? – Parris