我需要检查“成功”是真还是假.我从动作中得到以下json响应:
{“success”:true}
我怎么能检查它是真还是假.我试过这个,但它不起作用.它回来了未定义
- $.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);
- }
- });
解决方法
您的控制器操作应如下所示:
- [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);
- }
- }
- });