Question

Environment: Eclipse running Tomcat v7.0, working with the Stripes framework for Java, and submitting to the service via a JQuery ajax call.

The Javascript:

jQuery( function(){
jQuery('#testForm').submit( function(e) {
    e.preventDefault();

    var dataString = jQuery('#testInput').val();  //  Pulled from a textarea for testing purposes.
    var urlString = jQuery('#urlDropdown').val();  //  Pulled from a dropdown for testing purposes.
    if( (jQuery('#urlId') != "" )   &&
        (!isNaN(parseInt( jQuery('#urlId').val() )) )   ){  //  Pulled from an input type=text for testing purposes.
        urlString += '?id=' + parseInt( jQuery('#urlId').val() );
    }
    alert("urlString:  " + urlString);
    jQuery.ajax({  
        type: "POST",  
        url: urlString,
        data: dataString,  
        dataType:  'json',
        success: function( returnData ) {  
            jQuery('#content').html(JSON.stringify(returnData));
        },
        fail: function( returnData ) {  
            alert("FAIL");
        }
    });  
});
});

The Stripes Interceptor:

@Before(stages=LifecycleStage.BindingAndValidation, on={"setClient"})
private Resolution intercept() {
    String rbody = getRequestBody();
    log.info("BODY: " + rbody);

    this.setBody( rbody );

    return null;
}

And the getRequestBody method being used:

protected String getRequestBody(){
    StringBuffer body = new StringBuffer();
    String line = null;
    try {
        BufferedReader reader = getContext().getRequest().getReader();
        while ((line = reader.readLine()) != null)
            body.append(line);
    } catch (Exception e) {
        log.error("Buffered Reader Failed", e);
        e.printStackTrace();
    }

    log.info("BODY: " + body.toString());
    return body.toString();
}

I am using Firebug to test the input, and the post body of the request is indeed filled with meaty json.

The log.info calls there output a completely empty string. If I call up the getContentLength() on the getRequest, it tells me that the content has the appropriate number of characters. But the content itself comes out as null.

I am 99.99% sure that nowhere else in the code is the Request body being consumed. At the moment, this is my only action file in the Stripes framework, as I've removed every single other file.

Somehow, the request body is completely empty. It should be full of meaty json. Help me, Stack Overflow, you're my only hope!

Was it helpful?

Solution

Thanks to the fine people on the Stripes IRC channel, I have an answer! I needed to add in contentType: "application/json", as so:

jQuery.ajax({  
    type: "POST",  
    url: urlString,
    contentType: "application/json",
    data: dataString,
    dataType:  'json',
    processData: false,
    success: function( returnData ) {  
        jQuery('#content').html(JSON.stringify(returnData));
    },
    fail: function( returnData ) {  
        alert("FAIL");
    }
});  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top