Search code examples
javaajaxservletsform-data

Server can't parse something in ajax post parameter, what is causing this?


I'm making an ajax post request, and i can see the value is being passed in the headers on the client side.

However on the server side the value received is blank? The server seems to be choking on something in the value when it tries to parse it. Because if i use encodeURIComponent it works fine. What is causing this behaviour?

<script>

    //var body = getDataFromTheEditor(); //doesn't work
    //var body = JSON.stringify(getDataFromTheEditor()); //doesn't work
    var body = encodeURIComponent(getDataFromTheEditor()); //works but don't want to do this...

    var params = {body: body};
    $.post("../../../../CommitEdit", $.param(params));

</script>

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String body = request.getParameter("body");
    System.out.println(body); //blank if i pass it normally :(

}

I've copied the formdata directly from the header as it is being passed (so there's something in here which the server doesn't like apparently):

pastebin - unparsed formdata

pastebin - parsed formdata

Relevant request headers:

Content-Length: 64488
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

Any ideas? Thanks.


Solution

  • The problem was because the string was too long. Apparently the console in Eclipse has a character limit. Which is why i was seeing a blank value. This question here helped solve the issue:

    Character limit for System.out.println() in Java

    The solution is to:

    1. Go to Window > Preferences > Run/Debug > Console
    2. Uncheck "Limit Console Output" (Alternatively you can increase the Console buffer size.)