0
私は、ウェブページから投稿要求を介してサーバーにデータを送信しています。Ratpackがボディから値を取得できません
$("#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で応答してください。例えば、あなたがあまりにもそれをチェーンでき
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");
})
:代わりにExecHarnessの (第2の目的は、前のオブジェクトデータに依存)