如何从spring获取参数响应?(RESTJavaScript)
我在将错误返回到html时遇到问题。所以,我有一个带有“sql解释器”的web应用程序 HTML如何从spring获取参数响应?(RESTJavaScript),javascript,java,spring,Javascript,Java,Spring,我在将错误返回到html时遇到问题。所以,我有一个带有“sql解释器”的web应用程序 HTML <button type="submit" onclick="executeSQL('interpreterSQL')"> <i class="fas fa-bolt"></i> </button> <textarea id="interpreterSQL" placeholder="❔❔❔"></textarea>
<button type="submit" onclick="executeSQL('interpreterSQL')">
<i class="fas fa-bolt"></i>
</button>
<textarea id="interpreterSQL" placeholder="❔❔❔"></textarea>
之后,我在我的服务中处理查询,并在我的控制器中返回要发布的消息:
控制器(春季发布)
用于执行本机查询的服务
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;
}
公共字符串executeSQL(字符串[]拆分){
SessionFactory hibernateFactory=someService.getHibernateFactory();
Session Session=hibernateFactory.openSession();
字符串消息=null;
对于(int i=0;i连接.prepareStatement(查询).execute());
message=“成功”;
}
捕获(例外e){
message=((SQLGrammarException)e).getSQLException().getMessage();
}
}
session.close();
返回消息;
}
最后,我们在我的控制器中,它准备返回值,我们有一条消息,其中包含关于sql异常的信息。我们在那里:
我的问题是:如何获得可变的“反馈”作为回应?
我需要在那里处理这个值,我认为:
但是“var response=xhttp.responseText”返回了我所有的HTML代码。我只需要控制器的参数“反馈”。
伙计们,有人能帮忙吗(我不知道如何发送该参数作为返回并在javascript中处理…也许您可以在
ModelAndView上更改controller方法以返回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);
}
我试过:(但我的控制台出现了错误,错误为500:它是否工作,ModelAndView至少没有给出500个错误?它“工作”的方式是:它返回整个html代码我也在使用thymeleaf,如果我尝试“用thymeleaf处理响应”,我将能够在这里看到“反馈”:但在我的页面上是“跨度”为空,仅在该响应中可见。请共享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;
}
@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);