【问题标题】:Ajax page refresh after passing values to server将值传递给服务器后刷新 Ajax 页面
【发布时间】:2014-04-08 08:32:40
【问题描述】:

我有以下代码 onClick of a button

function Test(){
 $.ajax({
 type: 'POST',
 url: 'testme?userId=
 <%=session.getAttribute("username").toString()%>',
 load: 'test.jsp'
 });
}

我想通过上面实现的是将值传递给服务器后,我想使用 ajax 刷新页面。但是页面没有刷新。

我该如何解决这个问题?

编辑 1

<div id="myDiv">
<iframe src='testiframe.jsp?prod=<%=request.getParameter("prod")%>'
id="maxim" width="100%" 
frameborder="0" height="190"></iframe>
</div>  

【问题讨论】:

  • 您要刷新页面的哪个部分?这只是从 Ajax 调用中获取返回的数据并替换部分(或全部)页面的问题。例如您可以返回当前页面并替换所有 HTML,或者您可以(应该)只返回更改的部分并替换它。 Ajax 就是为了改变最小值(并发送最少的数据量)。
  • @TrueBlueAussie 我的页面有两个部分,一个有按钮,下面是一个 iframe。我想刷新 iframe 部分。

标签: jquery ajax jquery-easyui


【解决方案1】:

如果你使用ajax,是因为你不想刷新页面。但由于某种原因,如果它是你想要的,你可以这样做:

function Test(){
    $.ajax({
    type: 'POST',
    url: 'testme?userId=<%=session.getAttribute("username").toString()%>',
    success: function(){
        location.reload();
    }
 });
}

【讨论】:

    【解决方案2】:

    在你的ajax中使用window.location.hrefsuccess()

    function Test(){
      $.ajax({
       type: 'POST',
       url: 'testme?userId=<%=session.getAttribute("username").toString()%>',
       success: function(){
       window.location.href= 'test.jsp';
      }
    });
    }
    

    【讨论】:

      【解决方案3】:

      假设,您有myContentDiv div 来刷新来自服务器的新内容,然后在成功回调中更新其内容。默认情况下,dataType 为 html,这意味着您将从服务器返回 html 内容

      function Test(){
       $.ajax({
         type: 'POST',
         url: 'test.jsp?userId=<%=session.getAttribute("username").toString()%>',
         success: function(data){
            $('#myContentDiv').html(data);
         }
        });
      }
      

      【讨论】:

      • 我要刷新的部分是在 div 中定义的 iframe。我也想将参数传递给 iframe 页面,否则页面将不会重新加载。我已经用 iframe 部分编辑了我的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 2021-01-29
      • 1970-01-01
      • 2013-01-12
      相关资源
      最近更新 更多