jquery – Mvc json响应检查true / false

前端之家收集整理的这篇文章主要介绍了jquery – Mvc json响应检查true / false前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要检查“成功”是真还是假.我从动作中得到以下json响应:

{“success”:true}

我怎么能检查它是真还是假.我试过这个,但它不起作用.它回来了未定义

  1. $.post("/Admin/NewsCategory/Delete/",{ id: id },function (data) {
  2. alert(data.success);
  3. if (data.success) {
  4. $(this).parents('.inputBtn').remove();
  5. } else {
  6. var obj = $(this).parents('.row');
  7. serverError(obj,data.message);
  8. }
  9. });

解决方法

您的控制器操作应如下所示:
  1. [HttpPost]
  2. public ActionResult Delete(int? id)
  3. {
  4. // TODO: delete the corresponding entity.
  5. return Json(new { success = true });
  6. }

就个人而言,我会使用HTTP DELETE动词,这似乎更适合删除服务器上的资源,并且更加RESTful:

  1. [HttpDelete]
  2. public ActionResult Delete(int? id)
  3. {
  4. // TODO: delete the corresponding entity.
  5. return Json(new { success = true,message = "" });
  6. }

然后:

  1. $.ajax({
  2. url: '@Url.Action("Delete","NewsCategory",new { area = "Admin" })',type: 'DELETE',data: { id: id },success: function (result) {
  3. if (result.success) {
  4. // WARNING: remember that you are in an AJAX success handler here,// so $(this) is probably not pointing to what you think it does
  5. // In fact it points to the XHR object which is not a DOM element
  6. // and probably doesn't have any parents so you might want to adapt
  7. // your $(this) usage here
  8. $(this).parents('.inputBtn').remove();
  9. } else {
  10. var obj = $(this).parents('.row');
  11. serverError(obj,result.message);
  12. }
  13. }
  14. });

猜你在找的jQuery相关文章