问题
I try do POST some data to a Wicket WebPage. This works fine if the data is in a form. However, I want to post the data with jQuery's ajax-post. I am unable to get this data in my Page-constructor.
This is my jquery command:
$.ajax({
type: "post",
cache: false,
url: "http://localhost:8888/testjson",
data: JSON.stringify({"aap":"noot"),
contentType: 'application/json',
success: function(ydata){alert("aap");},
failure: function(errMsg) {alert(errMsg);},
contentType: false,
dataType: "json"
});
The /testjson is a mounted WebPage.
public TestJsonApiPage( PageParameters pp )
{
try
{
byte[] data = IOUtils.toByteArray( ( (ServletWebRequest) TestJsonApiPage.this.getRequest() ).getContainerRequest().getInputStream() );
}
catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
This is the constructor. What I see happening is that the inputstream is empty. However, when debugging, I see the raw data that I posted in HttpServletRequest
in the newWebRequest
in my WicketApplication
tl;dr How to get the raw post data in Wicket Page?
回答1:
It seems Page
does something to the post-parameters.
The solution for my problem is to use a Resource.
public class MyResource extends AbstractResource
@Override
protected ResourceResponse newResourceResponse( Attributes attributes )
{
ResourceResponse resourceResponse = new ResourceResponse();
resourceResponse.setContentType( "text/json" );
resourceResponse.setTextEncoding( "utf-8" );
HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
try
{
this.json = IOUtils.toString( request.getInputStream() );
}
catch ( IOException e )
{
e.printStackTrace();
}
resourceResponse.setWriteCallback( new WriteCallback()
{
@Override
public void writeData( Attributes attributes ) throws IOException
{
OutputStream outputStream = attributes.getResponse().getOutputStream();
Writer writer = new OutputStreamWriter( outputStream );
writer.write( MyResource.this.json );
writer.close();
}
} );
return resourceResponse;
}
来源:https://stackoverflow.com/questions/17874695/wicket-http-post-get-raw-data-from-servletrequest