我是通过发布请求从网页发送数据到服务器。
$("#1, #2, #3, #4").on("click", function(){
console.log($(this).attr("id"));
var xhr = new XMLHttpRequest();
xhr.open("POST", "SimpleServlet.html", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(JSON.stringify({"product_id": $(this).attr("id"), "quantity" : 1 }));
});在这个javascript的帮助下。我确信它会被发送到服务器并到达那里。
在服务器上,我试图检索写入数据的值。
.post("SimpleServlet.html", ctx ->
{
final Response response = ctx.getResponse();
System.out.println("Getting result");
final ExecResult<String> result = ExecHarness.yieldSingle(c ->
ctx.parse(String.class));
System.out.println("Getting value");
response.send("webshop.html");
})不幸的是,我没有找到任何指南来相应地检索字符串值。
我尝试了上面的方法,但这确实永远停留在ExecHarness中。
我想得到这些价值。带它们创建一个新的java对象,然后返回另一个java对象的json响应。(第二个对象取决于以前的对象数据)
发布于 2017-04-13 11:11:41
Ratpack的API参考而不是ExecHarness尝试如下所示:
ctx.getRequest().getBody().then({ data ->
String text = data.getText();
// parse text with whatever you use, e.g. Jackson
System.out.println("Getting value");
response.send("webshop.html");
})你也可以把它拴起来。
context.getRequest().getBody().flatMap({ data ->
//parse data and get id
//call to async service who returns Promise
return service.getReport(id).map((String report) -> {
// do some staff
})
}).then({
//final staff before send response
//import static ratpack.jackson.Jackson.json;
context.getResponse().send(json(result).toString());
})https://stackoverflow.com/questions/43309771
复制相似问题