P
P
parkito2016-09-02 23:58:04
JavaScript
parkito, 2016-09-02 23:58:04

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>


I want to pass value from jsp to this function

<%contracts.get(i).getNumber();%>

, but it doesn't work like this
<form name="test" onclick="unblock(<%contracts.get(i).getNumber();%>)">
                        <button type="submit" class="btn btn-success">unblock</button>
                    </form>


The script itself
<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>


Tried using request parameters - it doesn't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evhen, 2016-09-03
@parkito

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

JSP page
<%@ 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 question

Ask a Question

731 491 924 answers to any question