JQuery Ajax Post - 操作 returns 部分查看但未成功或显示
JQuery Ajax Post - Action returns Partial View but does not hit success or displays
我正在如下调用控制器操作:
$j.ajax({
url: url,
type: "post",
data: JSON.stringify(commentParam),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data, status, jqxhr) {
$j('#' + commentsCtrlId + ' ul').prepend(data);
commentField.val('');
},
failure: function () {
alert("Error adding comment");
}
});
操作看起来像:
[HttpPost]
public ActionResult Create(string commentBody, int parentObjectId, string parentObjectType, int actionId = 0)
{
try
{
// code to check security and pass info to the database. A comment model object is then passed back to the partial view
return PartialView("_Comment", comment);
}
catch (Exception ex)
{
Logger.Instance.LogError("Error in CommentController Create", ex);
return View("Error");
}
}
我可以打破这个方法,数据传递到数据库没问题。
我可以在局部视图上打断点,这样就可以看到数据在那里正常传递,但是当我返回我的 ajax 调用时,它永远不会得到响应(无论是成功还是失败)!我在任何地方都没有收到任何错误,而且绝对没有任何事情可以继续。有人对我至少可以尝试调试这里发生的事情有什么建议吗?
您的方法 return 是一个视图 (html),因此您需要将 dataType
选项更改为 'html'
$j.ajax({
url: url,
type: "post",
data: JSON.stringify(commentParam),
dataType: 'html', // modify
contentType: "application/json; charset=utf-8",
success: function (data, status, jqxhr) {
另请注意,您的方法应该 return 在 catch
块中使用 PartialView
(就像您对 try
块所做的那样)
catch (Exception ex)
{
Logger.Instance.LogError("Error in CommentController Create", ex);
return PartialView("Error"); // modify
}
我正在如下调用控制器操作:
$j.ajax({
url: url,
type: "post",
data: JSON.stringify(commentParam),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data, status, jqxhr) {
$j('#' + commentsCtrlId + ' ul').prepend(data);
commentField.val('');
},
failure: function () {
alert("Error adding comment");
}
});
操作看起来像:
[HttpPost]
public ActionResult Create(string commentBody, int parentObjectId, string parentObjectType, int actionId = 0)
{
try
{
// code to check security and pass info to the database. A comment model object is then passed back to the partial view
return PartialView("_Comment", comment);
}
catch (Exception ex)
{
Logger.Instance.LogError("Error in CommentController Create", ex);
return View("Error");
}
}
我可以打破这个方法,数据传递到数据库没问题。
我可以在局部视图上打断点,这样就可以看到数据在那里正常传递,但是当我返回我的 ajax 调用时,它永远不会得到响应(无论是成功还是失败)!我在任何地方都没有收到任何错误,而且绝对没有任何事情可以继续。有人对我至少可以尝试调试这里发生的事情有什么建议吗?
您的方法 return 是一个视图 (html),因此您需要将 dataType
选项更改为 'html'
$j.ajax({
url: url,
type: "post",
data: JSON.stringify(commentParam),
dataType: 'html', // modify
contentType: "application/json; charset=utf-8",
success: function (data, status, jqxhr) {
另请注意,您的方法应该 return 在 catch
块中使用 PartialView
(就像您对 try
块所做的那样)
catch (Exception ex)
{
Logger.Instance.LogError("Error in CommentController Create", ex);
return PartialView("Error"); // modify
}