简体   繁体   中英

How to get parametr in response from spring? (rest Javascript)

I have a problem with returning an error to html. So, I have web-app with "sql interpreter".

HTML

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

在此处输入图片说明

After entering a query into the interpreter, I run POST in javascript and shoot to spring:

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

After that I handle the query in my service and return message to POST in my Controller:

Controller (POST in Spring)

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

Service which is used to execute native query

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

So finally we are in my controller which is ready to return value and we have message which is have information about sql exceptions. We are there:

在此处输入图片说明

And here is my question: How to get variable "feedback" in response?

I need to handle that value there i think: 在此处输入图片说明

but that "var response = xhttp.responseText" is returning all my HTML code. I need only parametr "feedback" from my controller. Guys can someone help? :( I don't know how to send that parametr in return and handle it in javascript...

Maybe you can change your Controler method to return JSON response instead on 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);
}

Now you should be able to see the status

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM