Why is Ajax request aborted?

by user3698042   Last Updated May 16, 2019 00:26 AM

I want to send Ajax request to server and get HTML text. I found that the server successfully receive the request and work with DB, but Ajax request is aborted at some point and fail to load data. I have no clue what happens..

JS

const listGroup = document.getElementById("list-group");

    function sendData() {
        const form = document.getElementById("create-memo-form");
        const formData = new FormData(form);
        const XHR = new XMLHttpRequest();
        let urlEncodedData;
        const urlEncodedDataPairs = [];

        for(let pair of formData) {
            urlEncodedDataPairs.push(encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1]));
        }

        urlEncodedData = urlEncodedDataPairs.join('&').replace(/%20/g, '+');



        XHR.addEventListener('loadstart', function() {
            console.log("loadstart");
        });
        XHR.addEventListener('load', function() {
            console.log("load");
        });
        XHR.addEventListener('loadend', function() {
            console.log("loadend");
        });
        XHR.addEventListener('progress', function() {
            console.log("progress");
        });
        XHR.addEventListener('error', function() {
            console.log("error");
        });
        XHR.addEventListener('abort', function() {
            console.log("abort");
        });

        XHR.onreadystatechange = function() {
            console.log(XHR.readyState + ' ' + XHR.status);
            if(XHR.readyState === XMLHttpRequest.DONE) {
                console.log(XHR.getAllResponseHeaders());
            }

            if (XHR.readyState === XMLHttpRequest.DONE && XHR.status === 200 ) {

                console.log(XHR.responseText);
                //listGroup.removeChild(listGroup.childNodes[0]);
                //listGroup.innerHTML = XHR.responseText + listGroup.innerHTML;
            }
        };


        XHR.open('POST', 'create-memo');

        XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        XHR.send(urlEncodedData);
    }

createMemoForm.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="list-group-item">
    <form:form modelAttribute="newMemo" id="create-memo-form">
        <form:input path="content" /><br>
        <button onclick="sendData()">Write</button><br>
        <form:errors cssClass="error" />
    </form:form>
</div>

createMemoSuccess.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="createMemoForm.jsp" />
<div class="list-group-item">
    <h4 class="list-group-item-heading">${createdMemo.content}</h4>
    <div class="memo-btn-group">
        <span class="write glyphicon glyphicon-pencil"></span>
        <span class="glyphicon glyphicon-trash">Delete</span>
        <span class="label ${createdMemo.cssClass}">${createdMemo.priorityString}</span>
        <span>${createdMemo.deadline}</span>
    </div>
</div>

Controller

@RequestMapping("/create-memo")
    public String createMemo(Model model, @ModelAttribute("newMemo") Memo newMemo, BindingResult bindingResult, HttpSession session) {
        System.out.println("inside createMemo method");
        Member currentMember = (Member) session.getAttribute("currentMember");
        newMemo.setMemberId(currentMember.getId());
        if(memoService.addMemo(newMemo)) {
            model.addAttribute("newMemo", new Memo());
            model.addAttribute("createdMemo", newMemo);
            System.out.println("A");
            return "createMemoSuccess";
        } else {
            bindingResult.reject("create-memo-failure", "Failed to write");
            System.out.println("B");
            return "createMemoForm";
        }
    }

Firefox console printed these lines:

2 0
4 0

abort 
loadend
Tags : ajax spring jsp


Related Questions


Updated July 21, 2017 13:26 PM

Updated May 11, 2018 09:26 AM

Updated September 03, 2018 19:26 PM

Updated May 21, 2017 09:26 AM

Updated July 06, 2017 03:26 AM