Java 如何在ajax操作后刷新jsp页面

Java 如何在ajax操作后刷新jsp页面,java,javascript,mysql,ajax,jsp,Java,Javascript,Mysql,Ajax,Jsp,我的问题是我在jsp页面中有一个html表。我应用了拖放技术进行行排序。我还将新的顺序保存到DB(Mysql)通过AJAX.NET调用操作,并使用order By sql查询显示顺序。但这是第二次,由于我无法获得TR id的新行顺序,因此无法正常工作。请先生分享您对此的看法。我正在通过Javascript代码进行拖放,如下所示: this.onDrop = function(table, droppedRow ) { var rows = this.table.tBodies[0].

我的问题是我在jsp页面中有一个html表。我应用了拖放技术进行行排序。我还将新的顺序保存到DB(Mysql)通过AJAX.NET调用操作,并使用order By sql查询显示顺序。但这是第二次,由于我无法获得TR id的新行顺序,因此无法正常工作。请先生分享您对此的看法。我正在通过Javascript代码进行拖放,如下所示:

  this.onDrop = function(table, droppedRow ) {
    var rows = this.table.tBodies[0].rows;
    var debugStr = "";
    for (var i=0; i<rows.length; i++) {
        debugStr += rows[i].id+" ";
        alert(debugStr);
        alert(droppedRow.id);
    }
    // document.getElementById('debug').innerHTML = debugStr;
    function ajaxRequest(){
        var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
        if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
            for (var i=0; i<activexmodes.length; i++){
                try{
                    return new ActiveXObject(activexmodes[i])
                }
                catch(e){
                //suppress error
                }
            }
        }
        else if (window.XMLHttpRequest) // if Mozilla, Safari etc
            return new XMLHttpRequest()
        else
            return false
    }

    //Sample call:
    var mypostrequest=new ajaxRequest()
    mypostrequest.onreadystatechange=function(){
        if (mypostrequest.readyState==4){
            if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
                document.getElementById("gfdg").innerHTML=mypostrequest.responseText
            }
            else{
                alert("An error has occured making the request")
            }
        }
    }
    //var namevalue=encodeURIComponent(document.getElementById("name").value)
    // var agevalue=encodeURIComponent(document.getElementById("age").value)
    var parameters="array="+debugStr+"&maxLimit="+droppedRow.id
    mypostrequest.open("POST", "tableAjaxUpdate.action", true)
    mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    mypostrequest.send(parameters)
}
this.onDrop=函数(表,droppedRow){
var rows=this.table.tBodies[0]。行;
var=”;
对于(var i=0;i

据我所知,在ajax调用之后,您并没有获得所需的输出。 我给你们一些链接,通过完整的概念理解和问题的解决方案,即在jsp上实现ajax调用,我们可以为你们提供这些链接

AJAX的概念流程图:AJAX在网页上的工作原理

如果您已经知道以上内容…在jsp上实现AJAX…这里是许多可能的解决方案之一。。。

下面是stackoverflow的线程,仅限于此。 上面的链接还提供了其他可能的解决方案


享受编码…:)

您可以使用


location.reload(true)

好吧,如果AJAX调用成功,您需要刷新页面。因此,在您的AJAX调用中,我写为:

var mypostrequest=new ajaxRequest();
mypostrequest.onreadystatechange=function(){
    if (mypostrequest.readyState==4){
        if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
            document.getElementById("gfdg").innerHTML=mypostrequest.responseText;
            //this is the success point of your AJAX call and you need to refresh here 
            window.location.reload(); //this is the code for reloading
            //but your "gfdg" div data will be lost if you refresh,
            // so start another AJAX call here 
        }
        else{
            alert("An error has occured making the request");
        }
    }
}
但是我担心您的
gfdg
div在重新加载页面后会丢失一些新数据。您可以使用另一个AJAX调用来代替刷新


还有一点,您使用的是经典的AJAX,而不是更高级的库,如。它将简化您的代码,并具有很大的灵活性和浏览器兼容性。

AJAX用于在不刷新页面的情况下获取一些服务器端数据。如果要刷新页面,为什么要使用AJAX?请提交一个
。如果您的要求是在AJAX调用完成后刷新页面,然后在Javascript中也有一些解决方法。但是如果不看到您正在做什么,很难给出一些建议。如果您仍然有问题,请发布您的代码。先生,当我拖动一行并将其放在其他位置时,此Javascript函数this.onDrop=函数(表,droppedRow)正在调用,因此为了更新到服务器新行顺序,我必须调用AJAX。您可以在我的帖子中看到代码。在执行操作后,我再次返回相同的jsp,但这次我在UI中没有再次获得行id。请先生与您共享视图,我在其中挣扎了很多天。我必须在我的cade中添加此代码以再次刷新页面。先生我在上面发布了我的代码,请留意一下。我不知道你到底想在哪里刷新jsp页面。无论如何,你可以做一件事,像下面这样编写java脚本函数test(){location.reload(true);}如果您有更多疑问,请在此处发布。当服务器返回响应时,我想再次加载上面的html代码。确定后,将其放在此处…如果(mypostrequest.status==200 | | | window.location.href.indexOf(“http”)=-1{document.getElementById(“gfdg”).innerHTML=mypostrequest.responseText;location.reload(true);}先生,这种情况就像我把上面的html代码放在单独的jsp(call.jsp)中,并包含这个jsp(call.jsp)现在我正在拖放表的行并更新服务器。并在gfdg div中返回call.jsp,但我的gfdg div没有刷新为什么?AJAX调用真的返回了一些值吗?如果执行
警报(mypostrequest.responseText);
var mypostrequest=new ajaxRequest();
mypostrequest.onreadystatechange=function(){
    if (mypostrequest.readyState==4){
        if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
            document.getElementById("gfdg").innerHTML=mypostrequest.responseText;
            //this is the success point of your AJAX call and you need to refresh here 
            window.location.reload(); //this is the code for reloading
            //but your "gfdg" div data will be lost if you refresh,
            // so start another AJAX call here 
        }
        else{
            alert("An error has occured making the request");
        }
    }
}