Submit form with jquery ajax

前端 未结 4 1298
孤城傲影
孤城傲影 2020-12-24 14:56

I\'m trying to learn MVC and one the things I want to do is submit a form to an action in my controller and this action will return the submitted data. Sounds simple but I\'

4条回答
  •  既然无缘
    2020-12-24 15:35

    Basically you are passing javascript object literals directly. So, before you pass data to your controller, it must be in JSON format(because you have specified application/json. see your $.ajax call).
    SO, you are missing JSON.stringify()

    data: JSON.stringify({
                            PostId: $('.postid').val(),
                            UserName: $('#username').val(),
                            DateCreated: new Date(),
                            CommentText: $('#comment').val()
                        }),
    

    OR

    var someObj = {
                PostId: $('.postid').val(),
                UserName: $('#username').val(),
                DateCreated: new Date(),
                CommentText: $('#comment').val()
            };
    
             $.ajax({
                /// your other code
                data: JSON.stringify(someObj),
                // your rest of the code
    
            });
    

提交回复
热议问题