0

I'm creating an AJAX function that can make a request for insert a new line in a database and then expose the result in the browser web of the client. So I made this things:

script.js

/* bla bla bla*/

function submitAjaxFunction() {

    var data = { /*some data*/ };

    $.ajax({
        type: "POST",
        url: "/*my url*/",
        data: data,
        success: function(response){
            /*add a new row in #results*/
        },
        error: function(e){
            /*things*/
        }
    });
}

/* bla bla bla*/

page.html

/* bla bla bla*/

<input type="button" value="Insert new Line">

/* bla bla bla*/

<table id="results">
    /* list of row in the database */
</table>

Logic.java

public class Logic extends AuthorizedAction {

    /* bla bla bla*/

    public ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        /* bla bla bla*/

        String result = null;

        /* things for fill result */

        response.setContentType("text/text;charset=utf-8");
        response.setHeader("cache-control", "no-cache");
        PrintWriter out = response.getWriter();
        out.println(result);
        out.flush();
        return null;
    }
}

The problem is that the logic behind the insertion of a new row (essentially the Java method) is quite time-expending. So, we can have two scenarios:

A) The waiting user don't do anyting untill the logic end: in this case "submitAjaxFunction" work perfectly and it add the new row whitout the page refresh;

B) The waiting user refresh the page before the logic end: in this case we "lost" the response and the function "submitAjaxFunction" can't add the new row in #results. For see the new row, the user will have to refresh the page again;

The second scenario is not what good for me. Is it possible to get the AJAX response even if the page was refreshed?

Thanks

2
  • " Is it possible to get the AJAX response even if the page was refreshed?"...no because the ajax request is part of the script running on the page. If you refresh the page, the previous page was destroyed and any script running on it was destroyed too. So you can't get the response back, although if the request had hit the server, it may still have been processed.
    – ADyson
    Commented Apr 20, 2018 at 13:29
  • If you display some sort of "processing" indicator for the duration of the ajax request - often an animated gif or similar, so that the user knows something is going on, they are much less likely to do anything silly like reload the page while it's happening. Lots of sites explicitly say things like "do not reload this page" while doing important things like purchase transactions
    – ADyson
    Commented Apr 20, 2018 at 13:30

2 Answers 2

1

I suppose the response from your java code is html string, not Json Response: If you wait for some values from the backend, you can do it with ajax.

<table id="results">

</table>

var data = { /*some data*/ };
$.ajax({
    type: "POST",
    url: "/*my url*/",
    data: data,
    success: function(response){
        // Refresh the whole table, not a good one, there's other powerful ways to do it. such as refresh only the affected row
        $("#results").replaceWith($("#results",response)); // or $("#results").html($("#results",response).html());

    },
    error: function(e){
        /*things*/
    }
});

If the backend has nothing to add to the dom, just append the table with other rows

var html =  "<tr><td></td>....</td>";
$("#results tbody").append(html);
2
  • even if the server returns html, it must have the data before it created the html, needed by frontend, we can do the further processing then
    – Umair Abid
    Commented Apr 20, 2018 at 13:44
  • definitely, this part of code $("#results").replaceWith($("#results",response)) replaces the dom, with what the response is
    – Lemayzeur
    Commented Apr 20, 2018 at 15:02
1

If you immediately want to do some operation after form submission I will suggest to not wait for the first request to finish and then initiate a second one, rather do it in the first.

If you need to initiate the second request based on some some parameters, then send them too, with the first request and process your logic in JAVA side

Not the answer you're looking for? Browse other questions tagged or ask your own question.