javascript - 如何获取spring响应的参数? (其余 JavaScript)

标签 javascript java spring

我在向 html 返回错误时遇到问题。所以,我有带有“sql 解释器”的网络应用程序。

HTML

<button type="submit" onclick="executeSQL('interpreterSQL')">
    <i class="fas fa-bolt"></i>
</button>
<textarea id="interpreterSQL" placeholder="❔❔❔"></textarea>

enter image description here

在解释器中输入查询后,我在 javascript 中运行 POST 并发送到 spring:

在 JavaScript 中发布

function executeSQL(interpreterSQL) {
    var tmp = document.getElementById(interpreterSQL).value;

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            // Typical action to be performed when the document is ready:
            var response = xhttp.responseText;
            console.log("ok"+response);
        }
    };

    xhttp.open("POST", "/user/executeSQL", true);
    xhttp.send(tmp);
}

之后,我在服务中处理查询并将消息返回到 Controller 中的 POST:

Controller (Spring 中的 POST)

@PostMapping(path = { "/user/executeSQL" })
public ModelAndView executeSQL(@RequestBody String tmp) {
    String[] split = tmp.replace("\n", "").replace("\t", "").split(";");
    String feedback = databaseTableService.executeSQL(split);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("successMessage", feedback);
    modelAndView.setViewName("/user/interpreterSQL");
    return modelAndView;
}

用于执行 native 查询的服务

public String executeSQL(String[] split){
    SessionFactory hibernateFactory = someService.getHibernateFactory();
    Session session = hibernateFactory.openSession();
    String message = null;
    for (int i = 0; i < split.length; i++) {
        try{
            String query = split[i];
            session.doWork(connection -> connection.prepareStatement(query).execute());
            message = "Success";
        }
       catch(Exception e){
            message = ((SQLGrammarException) e).getSQLException().getMessage();
       }

    }
    session.close();
    return message;
}

所以最后我们在我的 Controller 中,它准备返回值,并且我们有消息,其中包含有关 sql 异常的信息。我们在那里:

enter image description here

这是我的问题:如何获得变量“反馈”作为响应?

我认为我需要处理这个值: enter image description here

但是“var response = xhttp.responseText”正在返回我的所有 HTML 代码。我只需要来自 Controller 的参数“反馈”。 伙计们有人可以帮忙吗? :( 我不知道如何返回该参数并在 javascript 中处理它...

最佳答案

也许您可以更改您的 Controler 方法以返回 JSON 响应,而不是在 ModelAndView

@PostMapping(path = { "/user/executeSQL" })
public ResponseEntity<Object> executeSQL(@RequestBody String tmp) {
    String[] split = tmp.replace("\n", "").replace("\t", "").split(";");
    Map<String,String> response = new HashMap<String, String>();
    response.put("feedback", databaseTableService.executeSQL(split));
    return new ResponseEntity<>( response , HttpStatus.OK);
}

现在您应该能够看到状态

var response = xhttp.responseText;
console.log("ok"+response);

关于javascript - 如何获取spring响应的参数? (其余 JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59802881/

相关文章:

java - java进程中的实际线程数是多少?

java - 找不到 MBean "jboss.web:type=Manager,path=/,host=localhost"

java - Spring 业务对象建模

java - Spring Boot 和 PostgreSQL- HikariCP 总是返回 null

javascript - 我可以在这个例子中立即调用我的函数而不是用匿名函数包装它吗?

Javascript:在 JavaScript 中是否有等同于 lua 的 _G?

javascript - 如何防止无限递归

java - 从 Java 中的字符串数组中删除未填充的值或空值

java - Spring Security,始终重定向到登录页面

javascript - jQuery UI (RAD/GUI) 表单设计器