Answer the question
In order to leave comments, you need to log in
How to establish connection between jpa and ajax?
Hello. Help, please solve the problem.
There is a form, it calls javascript
<form name="test" onclick="unblock()">
<button type="submit" class="btn btn-success">unblock</button>
</form>
<%contracts.get(i).getNumber();%>
<form name="test" onclick="unblock(<%contracts.get(i).getNumber();%>)">
<button type="submit" class="btn btn-success">unblock</button>
</form>
<script>
function unblock(id) {
popBox();
function popBox() {
x = confirm('Are you sure?');
if (x == true) {
var xhr = new XMLHttpRequest();
var id = 1;
xhr.open("POST", "/user/NumberOperations?unblockItem=" + id, true);
xhr.send();
}
}
}</script>
Answer the question
In order to leave comments, you need to log in
onclick="unblock(<% = contracts.get(i).getNumber();%>)" missed an equal sign in the handle.
function unblock() { - probably the function should take number or id?
function unblock(id) {
You can report the success or failure of the operation on the server using the status code in the response from the server.
For example Servlet
delete operation
@WebServlet("/test")
public class TestServlet extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(TestServlet.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/test.jsp").forward(req, resp);
}
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
long id = Long.parseLong(req.getParameter("id"));
testRemove(id);
resp.setStatus(200);
} catch (Exception e) {
LOG.error("Error remove: {}", e.getMessage());
resp.setStatus(500);
}
}
private void testRemove(long id) {
if (id < 1) {
throw new RuntimeException("id can't be < 1");
}
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>
<input type="button" value="remove data (OK)" onclick="remove(2)"/>
</p>
<p>
<input type="button" value="remove data (ERROR)" onclick="remove(-1)"/>
</p>
<script>
function remove(id) {
var request = new XMLHttpRequest();
request.open('DELETE', '/test?id=' + id, false);
request.send();
if (request.status == 200) {
alert('Данные упешно удалены')
} else {
alert('Ошибка удаления данных!');
}
}
</script>
</body>
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question