ASP.NET MVC AJAX with jQuery

后端 未结 2 675
Happy的楠姐
Happy的楠姐 2020-12-23 10:27

I have a site where each user\'s page shows comments and allows other user\'s to add comments. I want to have it so the add comments form is on the page and when a user adds

2条回答
  •  爱一瞬间的悲伤
    2020-12-23 11:08

    You would need to take advantage of the 'success' (or 'complete') event that is fired by the jQuery ajax call to fire a subsequent AJAX call for refreshing the content of your reviews. This would probably look something like (winged it, untested):

    function UpdateComments(){
        resultHTML = jQuery.ajax({
            type: 'GET',
            url: 'Comments/List/UserID'
        }).responseText;
    
        $('#comments').html(resultHTML);
    }
    
    function PostComment(targetUserID, commenterUserID, comment)
    jQuery.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: $.toJSON({review: comment, id:targetUserID, commenter:commenterUserID}),
            dataType: 'json',
            url: 'Comments/Add',
            success: function(result){
                // Only update comments if the post was successful:
                resultJson = $.evalJSON(result);
                if(resultJson['success'] == true){
                    UpdateComments();                    
                }
            }
        });
    

    EDIT The JSON code would make use of the jquery plugin jquery-json (http://code.google.com/p/jquery-json/)

提交回复
热议问题