Return a PartialView from $.Ajax Post

前端 未结 2 851
感动是毒
感动是毒 2020-12-14 02:47

I have the following code;

        $.ajax({
            url: \"/Home/jQueryAddComment\",
            type: \"POST\",
            dataType: \"json\",
                 


        
相关标签:
2条回答
  • 2020-12-14 03:38

    Unless it was copied over wrong it appears you are missing some closing tokens.

           $.ajax({
            url: "/Home/jQueryAddComment",
            type: "POST",
            dataType: "json",
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function(data){ 
                //var message = data.Message; 
                alert(data);
                $('.CommentSection').html(data);
                } //<-- added close for anonymous function
            }); //<--added close/semicolon for ajax function
    

    Also, you are POSTing but it your action doesn't appear to have the [Post] attribute. When you run this in the debugger does a breakpoint on your action get hit?

    0 讨论(0)
  • 2020-12-14 03:51

    Your expecting JSON in the .Ajax POST, but in the ActionMethod your returning a PartialView?

    Try:

    $.ajax({
       url: "/Home/jQueryAddComment",
       type: "POST",
       dataType: "html",
       data: json,
       success: function(data){ 
          //var message = data.Message; 
          alert(data);
          $('.CommentSection').html(data);
       }
    }
    
    0 讨论(0)
提交回复
热议问题