Spring MVC Ajax Post 400 Error (Bad Request)
<p>So I'm using Spring MVC and I'm trying to do an ajax post to add a <code>comment</code> to a <code>Post</code> entity, like a typical social network.  And I'm getting an error in the Chrome Developer's Tool's that says this <code>Failed to load resource: the server responded with a status of 400 (Bad Request)</code>.  I'm thinking that might mean something is going wrong in my controller, however the way this is set up, it's not letting me check it out in debug mode.</p>

<p>I'll show you guys all the pieces of code that work together so you guys can get a better understanding of my problem.</p>

<p>So here's my Ajax, and everything is running through, and sending a "there was an error" message, so It's at least running through the code and reaching the controller.</p>

<p>Also the <code>CDATA</code> stuff is for Thymeleaf.</p>

<pre><code>&lt;script th:inline="javascript"&gt;

        /*&lt;![CDATA[*/

        var postById = /*[[${postById.id}]]*/'1';

        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");

        $(document).ajaxSend(function(e, xhr, options) {
            xhr.setRequestHeader(header, token);
        });

        $(document).ready(function(){
        $("#submit").on("click", function(ev) {
            ev.preventDefault();
            $.ajax({
                url : "newComment",
                type : "post",
                data : {
                    "postById" : postById,
                    "newComment" : $("#newComment").val()
                },
                success : function(data) {
                    console.log(data);
                    location.reload();
                },
                error : function() {
                    console.log("There was an error");
                    //location.reload();
                }

            });
        });
      });
    /*]]&gt;*/

&lt;/script&gt;
</code></pre>

<p>Here's my Controller Get Method</p>

<pre><code>@RequestMapping(value="viewCourse/post/{postId}", method=RequestMethod.GET)
public ModelAndView postViewGet (@RequestParam(value = "pageSize", required = false) Integer pageSize,
        @RequestParam(value = "page", required = false) Integer page, @PathVariable Long postId) {
    ModelAndView modelAndView = new ModelAndView("post");
{  

// Evaluate page size. If requested parameter is null, return initial
// page size
int evalPageSize = pageSize == null ? INITIAL_PAGE_SIZE : pageSize;
// Evaluate page. If requested parameter is null or less than 0 (to
// prevent exception), return initial size. Otherwise, return value of
// param. decreased by 1.
int evalPage = (page == null || page &lt; 1) ? INITIAL_PAGE : page - 1;

//StudySet studySet = studySetRepo.findOne(studySetId);

//List &lt;Row&gt; rows = studySet.getRows();
//Set&lt;Row&gt; rowSet = new TreeSet&lt;Row&gt;(rows);

Post postById = postRepo.findOne(postId);

Comment comment = new Comment();

Page&lt;Comment&gt; postComments = commentService.findByPostOrderByIdDesc((postById), new PageRequest(evalPage, evalPageSize));
Pager pager = new Pager(postComments.getTotalPages(), postComments.getNumber(), BUTTONS_TO_SHOW);

modelAndView.addObject("postId", postId);
modelAndView.addObject("postById", postById);
modelAndView.addObject("postComments", postComments);
modelAndView.addObject("comment", comment);
modelAndView.addObject("selectedPageSize", evalPageSize);
modelAndView.addObject("pageSizes", PAGE_SIZES);
modelAndView.addObject("pager", pager);
return modelAndView;
}
}
</code></pre>

<p>Here's my Controller Post Method</p>

<pre><code>@RequestMapping(value="viewCourse/post/newComment", method=RequestMethod.POST)
public @ResponseBody Post newComment (@Valid @RequestParam Long postId, @RequestParam String newComment, ModelMap model, @AuthenticationPrincipal User user)
{
Post post = postRepo.findOne(postId);
Comment comment = new Comment();
comment.setComment(newComment);
comment.setPost(post);

comment.setDate(LocalDate.now());
comment.setTime(LocalTime.now());
comment.setDateTime(LocalDateTime.now());

comment.setUser(user);

user.getComments().add(comment);

post.getComments().add(comment);

commentRepo.save(comment);
Post savedPost = postRepo.save(post);

return savedPost;
}
</code></pre>

<p>Also I have some annotations in the entity objects, that could have something to do with it.</p>

<p>Here's my User Entity</p>

<pre><code>@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
@JsonManagedReference
public Set&lt;Comment&gt; getComments() {
    return comments;
}

public void setComments(Set&lt;Comment&gt; comments) {
    this.comments = comments;
}
</code></pre>

<p>Here's my Comment entity</p>

<pre><code>@ManyToOne
@JsonBackReference
public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}
</code></pre>

<p>Also here's a picture of my console in the Chrome Developer Tools, so you guys can see exactly what it's showing me.</p>

<p><a href="https://i.stack.imgur.com/UODNR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UODNR.png" alt="Chrome Console"></a></p>

<p>If anyone can see where I'm going wrong and point me in the right direction, that would be great, thanks in advance.</p>

<p>Also if you guys need to see any other code, just let me know.</p>