Javascript JQuery成功函数未启动
我有以下脚本。它运行,将变量传递给控制器,控制器正确执行,但不管出于什么原因,success函数都不会启动,因此不会刷新我的html。相反,错误会触发。关于原因,我没有什么好担心的。谢谢你的帮助Javascript JQuery成功函数未启动,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我有以下脚本。它运行,将变量传递给控制器,控制器正确执行,但不管出于什么原因,success函数都不会启动,因此不会刷新我的html。相反,错误会触发。关于原因,我没有什么好担心的。谢谢你的帮助 $(function() { $("#btnUpdateTick").unbind('click').click(function () { var currenttick = { "TicketID":@Html.Raw(
$(function() {
$("#btnUpdateTick").unbind('click').click(function () {
var currenttick =
{
"TicketID":@Html.Raw(Json.Encode(Model.TicketID)),
"Title": $("#Title").val(),
"Creator": $("#Creator").val(),
"StatusID": $("#StatusID").val(),
"Description": $("#Description").val(),
"newComment":$("#txtAddComment").val(),
Cat:
{
"CatID":$("#ddCurrTickCat").val()
}
}
//var newcomment = $("#txtAddComment").val();
var conv = JSON.stringify(currenttick);
$.ajaxSetup({cache:false});
$.ajax({
url: '@Url.Action("UpdateTicket", "HelpDesk")',
data: JSON.stringify({ticket:currenttick}),
type: "POST",
dataType: "json",
contentType: "application/json",
success: function (data) {
$("#loadpartial").html(data);
},
error: function (data){alert("turd")}
});
});
});
我的控制器:
[HttpPost]
public PartialViewResult UpdateTicket(Tickets ticket)
{
////Tickets.UpdateTicket(currenttick);
if (ticket.newComment != "")
{
Comments.addCommentToTicket(ticket.TicketID, ticket.newComment,UserPrincipal.Current.SamAccountName.ToString());
}
Tickets model = new Tickets();
ViewBag.CategoryList = Category.GetCategories();
ViewBag.StatusList = TicketStatus.GetStatusList();
model = Tickets.GetTicketByID(ticket.TicketID);
model.TicketComments = new List<Comments>();
model.TicketComments = Comments.GetCommentsForTicketByID(ticket.TicketID);
//model.TicketComments = Comments.GetCommentsForTicketByID(ticketID);
//ViewBag.TicketComments = Comments.GetCommentsForTicketByID(ticketID);
return PartialView("TicketDetails", model);
}
[HttpPost]
公共PartialViewResult更新(票证)
{
////Tickets.updateciket(currenttick);
如果(ticket.newComment!=“”)
{
Comments.addCommentToTicket(ticket.TicketID、ticket.newComment、UserPrincipal.Current.SamAccountName.ToString());
}
票证型号=新票证();
ViewBag.CategoryList=Category.GetCategories();
ViewBag.StatusList=TicketStatus.GetStatusList();
模型=Tickets.GetTicketByID(ticket.TicketID);
model.TicketComments=新列表();
model.TicketComments=Comments.GetCommentsForTicketByID(ticket.TicketID);
//model.TicketComments=Comments.GetCommentsForTicketByID(ticketID);
//ViewBag.TicketComments=Comments.GetCommentsForTicketByID(ticketID);
返回PartialView(“TicketDetails”,model);
}
您的控制器返回的是视图,而不是json
。您应该返回一个JsonResult
。试试这个:
[HttpPost]
public JsonResult UpdateTicket(Tickets ticket)
{
////Tickets.UpdateTicket(currenttick);
if (ticket.newComment != "")
{
Comments.addCommentToTicket(ticket.TicketID, ticket.newComment,UserPrincipal.Current.SamAccountName.ToString());
}
Tickets model = new Tickets();
ViewBag.CategoryList = Category.GetCategories();
ViewBag.StatusList = TicketStatus.GetStatusList();
model = Tickets.GetTicketByID(ticket.TicketID);
model.TicketComments = new List<Comments>();
model.TicketComments = Comments.GetCommentsForTicketByID(ticket.TicketID);
//model.TicketComments = Comments.GetCommentsForTicketByID(ticketID);
//ViewBag.TicketComments = Comments.GetCommentsForTicketByID(ticketID);
return Json(model);
}
[HttpPost]
public JsonResult UpdateCket(票证)
{
////Tickets.updateciket(currenttick);
如果(ticket.newComment!=“”)
{
Comments.addCommentToTicket(ticket.TicketID、ticket.newComment、UserPrincipal.Current.SamAccountName.ToString());
}
票证型号=新票证();
ViewBag.CategoryList=Category.GetCategories();
ViewBag.StatusList=TicketStatus.GetStatusList();
模型=Tickets.GetTicketByID(ticket.TicketID);
model.TicketComments=新列表();
model.TicketComments=Comments.GetCommentsForTicketByID(ticket.TicketID);
//model.TicketComments=Comments.GetCommentsForTicketByID(ticketID);
//ViewBag.TicketComments=Comments.GetCommentsForTicketByID(ticketID);
返回Json(模型);
}
如果要从ajax调用返回部分视图,请将您的ajax请求修改为:
$.ajax({
url: '@Url.Action("UpdateTicket", "HelpDesk")',
data: JSON.stringify({ticket:currenttick}),
type: "POST",
dataType: "html",
success: function (data) {
$("#loadpartial").html(data);
},
error: function (data){alert("turd")}
});
现在,ur success函数中的“数据”将从PartialViewResult()返回html。为什么不实际查看错误而不是抛出警报?你的控制器是什么样子的?警报(“屎”);-哈哈,请将您的ajax错误处理程序修改为:“函数(xhr,ajaxOptions,thrownError){alert(thrownError);}”);”因此我们得到了错误消息(并使用console.log)谢谢Ivan,我不确定如何获得错误来调试它。它返回时出现语法错误:无效字符将其更改为此,来自Ajax的错误仅表示内部服务器错误。然而,一切仍在按它应该的方式执行。它可能需要调整,我是从内存中完成的,现在还没有开发人员的计算机来测试。