【问题标题】:How to get parametr in response from spring? (rest Javascript)如何从弹簧响应中获取参数? (其余 Javascript)
【发布时间】:2020-01-18 17:04:30
【问题描述】:

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

HTML

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

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

JavaScript 中的 POST

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);
}

之后我在我的服务中处理查询并将消息返回到我的控制器中的 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;
}

用于执行原生查询的服务

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;
}

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

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

我认为我需要处理那个值:

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

【问题讨论】:

    标签: javascript java spring


    【解决方案1】:

    也许您可以更改您的 Controler 方法以在 ModelAndView 上返回 JSON 响应

    @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);
    

    【讨论】:

    • 我试过了 :( 但我的控制台出现错误,错误 500:imgur.com/o5AEX0Btextuploader.com/161ip
    • 它是否正常工作,至少没有使用 ModelAndView 给出 500 错误?
    • 它以这种方式“工作”:imgur.com/SPlE3xs 它返回整个 html 代码
    • 我也在使用百里香,如果我尝试“用百里香处理响应”,我将能够在这里看到“反馈”:imgur.com/iUfb6Y0 但在我的页面上“跨度”是空的,仅在该响应中可见
    • 请分享与 imgur.com/SPlE3xs 类似但带有标头的 ModelAndView 响应的标头
    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多