Json with problem, does not continue execution after request

1

Contortor code

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 usually inserted, however it does not run alert on sucess , what is wrong?

    
asked by anonymous 16.06.2014 / 21:24

1 answer

2

Try to give a return 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);
}

Why:

JQuery expects a data, a value, to go through success , as it was sent null nothing was sent and therefore the success

    
16.06.2014 / 21:30