【问题标题】:getJSON - How to print out the result, and how to retrieve input?getJSON - 如何打印结果,以及如何检索输入?
【发布时间】:2018-11-15 09:40:51
【问题描述】:

我实现了一些java函数。现在我必须通过 html 将结果呈现给用户。

public Object post()
{
    responseHeaders.put("Content-Type", "application/json");

    try
    {
        final User user = UC0_Login.getLoggedInUser(this);
        List<Measurement> mylist =  MeasurementService.getMeasurementsbyPatient(user.getUsername().toString());
        for(int i = 0; i < mylist.size(); i++) {
             Measurement mesPatient = mylist.get(i);
             DownloaderService.saveTxt(mesPatient);
            // System.out.println("Test Name:      " + mesPatient.getRequest().getPatient().getFirst_name().toString());
         }

    }
    catch(HttpException x)
    {
        x.printStackTrace();
        this.statusCode = x.getStatusCode();
        return "{ \"success\": false }";
    }
    catch(Exception x)
    {
        x.printStackTrace();
        this.statusCode = StatusCode.SERVER_ERROR_500;
        return "{ \"success\": false }";
    }
    String property = "java.io.tmpdir";
    String tempDir = System.getProperty(property);
    String result = "Your measurements have been saved at" + tempDir;

    return result;
}

我在 Java 类中实现了 post()。我还在后台创建了一个 HttpServer。如代码所示,我返回一个字符串。如何在 HTML 中打印出该字符串?以及如何从 HTML 获取输入(例如整数)到例如 get() 函数?谢谢!!!

【问题讨论】:

    标签: javascript java html json


    【解决方案1】:

    如果您基本上是在寻找 AJAX 请求和响应,这里是一个很好的学习方法。

    https://blog.garstasio.com/you-dont-need-jquery/ajax/#posting

    例如,

    HTML

    <p id="result"></p>
    
    <form id="frm1" action="/action_page.php">
      Your input: <input type="text" id="myInput"><br><br>
      <input type="button" onclick="myFunction()" value="Submit">
    </form>
    

    JAVASCRIPT

    var input = document.getElementById("myInput").value;
    xhr = new XMLHttpRequest();
    xhr.open('POST', 'Your URL Goes Here');
    xhr.onload = function() {
        if (xhr.status === 200) {
            var response = xhr.responseText;
            document.getElementById("result").innerHTML = response;
        }
        else if (xhr.status !== 200) {
            alert('Request failed.  Returned status of ' + xhr.status);
        }
    };
    xhr.send(encodeURI('input=' + input)); // Use your API param keys here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      相关资源
      最近更新 更多