我需要检查“成功”是真还是假。我从行动中得到以下json回复:
{ “成功”:真}
如果是真或假,我怎么检查它。我试过这个,但它不起作用。它回来未定义
$.post("/Admin/NewsCategory/Delete/", { id: id }, function (data) {
alert(data.success);
if (data.success) {
$(this).parents('.inputBtn').remove();
} else {
var obj = $(this).parents('.row');
serverError(obj, data.message);
}
});
答案 0 :(得分:25)
您的控制器操作应如下所示:
[HttpPost]
public ActionResult Delete(int? id)
{
// TODO: delete the corresponding entity.
return Json(new { success = true });
}
就我个人而言,我会使用HTTP DELETE动词,它似乎更适合删除服务器上的资源并且更加RESTful:
[HttpDelete]
public ActionResult Delete(int? id)
{
// TODO: delete the corresponding entity.
return Json(new { success = true, message = "" });
}
然后:
$.ajax({
url: '@Url.Action("Delete", "NewsCategory", new { area = "Admin" })',
type: 'DELETE',
data: { id: id },
success: function (result) {
if (result.success) {
// WARNING: remember that you are in an AJAX success handler here,
// so $(this) is probably not pointing to what you think it does
// In fact it points to the XHR object which is not a DOM element
// and probably doesn't have any parents so you might want to adapt
// your $(this) usage here
$(this).parents('.inputBtn').remove();
} else {
var obj = $(this).parents('.row');
serverError(obj, result.message);
}
}
});