我尝试将一些数据发布到Wicket网页。如果数据采用表格形式,则效果很好。但是,我想用jQuery的ajax- post发布数据。我无法在我的Page构造函数中获取此数据。
这是我的jquery命令:
$.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" });
/ testjson是已安装的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(); }
这是构造函数。我看到的是输入流为空。然而,在调试的时候,我看到了原始数据,我在贴HttpServletRequest在newWebRequest我的WicketApplication
HttpServletRequest
newWebRequest
WicketApplication
tl; dr 如何在Wicket页面中获取原始帖子数据?
似乎Page对后参数有所帮助。
Page
我的问题的解决方案是使用资源。
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; }