Hir*_*del 1 java ajax playframework playframework-2.2
嗨,我想在我的网站上按一个按钮发送一个字符串,使用ajax到Play框架中的java代码.我找不到一个简单的教程,只是解释了如何做到这一点.他们都在使用模板.让我说我的java方法是:
  public static Result upload() { }
我的按钮正在调用一个javascript方法,它在单击时从另一个输入获取一个String:
<input id="submit" type="submit" onclick="send();">
我没有测试,但这样的事情应该有效.
应用控制器
public static Result upload() {
    JsonNode node = request().body().asJson().get("stringField");
    String inputString = node.getTextValue();"
    System.out.println(inputString)      // prints the string from the form field
    return ok();
}
路线
POST        /uploadfoostring       controllers.Application.upload()
模板
<input type="text" id="string-field">
<input id="submit" type="submit" onclick="send();">
<script type = "text/javascript" >
  $('#submit').click(function(evt) {
      var inputString = $('#string-field').val();
      var obj = {
        stringField: inputString;
      };
      $.ajax({
        url: "@routes.Application.upload()",
        data: JSON.stringify(obj),
        headers: {
          'Content-Type': 'application/json'
        },
        type: 'POST',
        success: function(res) {
          if (res) {
            console.log("Success!");
          } else {
            console.log("Failed...");
          }
        }
      });
    }
</script>
| 归档时间: | 
 | 
| 查看次数: | 2299 次 | 
| 最近记录: |