-1

i'm using this code for delete comment .

but when i delete comment , it not show me alert in success . what's the problem ?

function DeleteComment(id) {
        jQuery.ajax({
            url: "/Admin/Comment/DeleteComment/" + id,
            dataType: 'json',
            success: function (data) {
                if (data === true) {
                    alert("نظر مورد نظر با موفقست حذف گردید");
                } else {
                    alert("خطایی رخ داده ، نظر حذف نشد . لطفا خطا را بررسی کنید");
                }
            }
        });
    }

** Edit **

public JsonResult DeleteComment(int id)
    {
        var deletecomment = _CommentService.CommentByID(id);
        if (_CommentService.RemoveComment(deletecomment))
        {
            return Json(true);
        }
        else
        {
            return Json(false);
        }
    }
Mr Coder
  • 13
  • 1
  • 9

1 Answers1

1

If your request method is GET, you have to use JsonRequestBehaviour.AllowGet, like this:

return Json(true,JsonRequestBehaviour.AllowGet);

If you look in the console, you will see an error.

Why do you need JsonRequestBehaviour.AllowGet ?

This is to protect against a very specific attack with JSON requests that return data using HTTP GET.

By default, MVC framework does not allow you to respond to an GET request method with a JSON object, but, if you need to send JSON in response to a GET verb, you have to allow this using JsonRequestBehaviour.AllowGet.

Community
  • 1
  • 1
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128