Json with problem, does not continue execution after request

Asked

Viewed 43 times

1

Code of the contorller

public JsonResult InsertComment(string description, int postID)
{
    try
    {
        Comment comment = new Comment
        {
            Content = description,
            DataCommented = DateTime.Now,
            PostID = postID,
            UserID = User.Identity.GetUserId()
        };
        db.Comments.Add(comment);
        db.SaveChanges();
    }
    catch (Exception)
    {


    }
    return Json(comment, JsonRequestBehavior.AllowGet);
}

Json code

$(function () {
    if (CommentInput.value != "") {
        $.ajax({
            type: 'GET',
            url: '/Comment/InsertComment',
            dataType: 'Json',
            cache: false,
            async: true,
            data: { postID: CommentInput.getAttribute('data-postid'),      description: CommentInput.value },
            success: function () {
                alert('oi');                     
            }
        });
        CommentInput.value = "";
    }
});

The comment is inserted normally, but does not execute the alert in the sucess, what is wrong?

1 answer

2


Try to give a feedback instead of null.

public JsonResult InsertComment(string description, int postID)
{
    try
    {
        Comment comment = new Comment
        {
            Content = description,
            DataCommented = DateTime.Now,
            PostID = postID,
            UserID = User.Identity.GetUserId()
        };
        db.Comments.Add(comment);
        db.SaveChanges();
    }
    catch (Exception)
    {


    }
    return Json(postID, JsonRequestBehavior.AllowGet);
}

Because:

Jquery expects a given, a value, to pass through success, how it was sent null nothing was sent and with that also was not executed the success

  • 1

    worked, would you have any explanation about that to add? I will give the answer as correct in 8 minutes.

  • Yes I will put! if ta returning null then there was no success

Browser other questions tagged

You are not signed in. Login or sign up in order to post.