简体   繁体   English

jQuery和AJAX-页面刷新后获取请求

[英]JQuery and AJAX - Get the Request after the page refresh

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. 我正在创建一个AJAX函数,该函数可以请求在数据库中插入新行,然后将结果显示在客户端的浏览器网络中。 So I made this things: 所以我做了这样的事情:

script.js 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 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. 问题在于,插入新行(本质上是Java方法)背后的逻辑非常耗时。 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; A) 等待中的用户在逻辑结束之前不做任何事情 :在这种情况下,“ submitAjaxFunction”可以正常工作,并且在页面刷新后添加了新行;

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. B) 等待中的用户在逻辑结束之前刷新页面 :在这种情况下,我们“丢失”了响应,函数“ submitAjaxFunction”无法在#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? 即使刷新页面也可以获得AJAX响应吗?

Thanks 谢谢

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 如果您需要基于一些参数来发起第二个请求,那么也将它们与第一个请求一起发送,并在JAVA端处理您的逻辑

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. 我想来自您的Java代码的响应是html字符串,而不是Json响应:如果您等待后端的某些值,则可以使用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 如果后端没有任何内容可添加到dom,只需将表添加其他行

var html =  "<tr><td></td>....</td>";
$("#results tbody").append(html);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM