Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax成功后动态更新/刷新html,无需重新加载页面 标题_Javascript_Jquery_Html_Ajax_Refresh - Fatal编程技术网

Javascript ajax成功后动态更新/刷新html,无需重新加载页面 标题

Javascript ajax成功后动态更新/刷新html,无需重新加载页面 标题,javascript,jquery,html,ajax,refresh,Javascript,Jquery,Html,Ajax,Refresh,我有一个更新部分,在调用页面时首先加载该部分,然后一个定时js函数将再次查找更新,我在一个数组对象中获得了更新;我的问题从这里开始——如何动态刷新html,比如如果我收到5次更新,那么我想创建5张卡片来显示这些更新,而目前我只有两张卡片 <c:forEach items="${updates['updates']}" var="update" varStatus="loopCounter"> <div class="list-group-item media-list">

我有一个更新部分,在调用页面时首先加载该部分,然后一个定时js函数将再次查找更新,我在一个数组对象中获得了更新;我的问题从这里开始——如何动态刷新html,比如如果我收到5次更新,那么我想创建5张卡片来显示这些更新,而目前我只有两张卡片

<c:forEach items="${updates['updates']}" var="update" varStatus="loopCounter">
<div class="list-group-item media-list">
    <div class="media-body">
        <div class="admin-cont">
        <p>${update.title}</p>
        <p>${update.field1}</p> <p>Designation-${update.field2}</p>
            <p>
                ${update.content}
            </p>
        </div>      
    </div>
</div>
</c:forEach>


<script>
function getUpdates(){          
    $.ajax({
        url : URL,
        type : type,
        data : dataObj,
        success : function(data) {          
            //do something here to refresh and set data in html
        }
    });
}
</script>

${update.title}

${update.field1}

Designation-${update.field2}

${update.content}

函数getUpdates(){ $.ajax({ url:url, 类型:类型, 数据:dataObj, 成功:函数(数据){ //在此处执行一些操作以刷新和设置html中的数据 } }); }
您必须在ajax中成功构建html代码。”“数据”是您从ajax调用中得到的

success : function(data) {          
            $('#yourDiv').append('whatever you want'); //For example
        }

您可以循环它并从ajax响应中提取尽可能多的卡片。

在ajax调用的成功函数中,您可以执行以下操作:

$('admin-cont').append(data);
参考:

如果要在插入新数据之前清除
div
,可以执行以下操作:

$('admin-cont').empty();

参考资料:

如果这个答案太晚,我表示歉意,但我想我还是应该为其他遇到这个问题的人提一提

第一步:创建函数“UpdatePage”脚本,如果您有自己的更新脚本,请跳过此步骤。假设在加载页面时在内部调用此函数。(如果调用了“$(document).ready(function()”,则有效)

第二步:添加一个事件监听器,在散列更改后清除计时器,否则您将陷入竞争状态,具体取决于您是否有一个带有Angularjs的单页模板

第三步:在Ajax(success)函数中调用“onGetObjSuccess”函数,仅在Ajax成功时才起作用

第四步:最后调用数据更新的最后一个函数,注意:'.append()'将继续向html元素添加数据,但不会删除它。'.replaceWith()'将自动/动态地用新数据替换旧数据

试试看

<script>
function updatePage() {
  // Calling Ajax function as soon page is loaded
  window.setTimeout(getUpdates, 200); 
  var interval = window.setTimeout(updatePage, 5000);
  // Use your update function here ^ instead or simply call:
  // yourUpdateScript();

  window.addEventListener('hashchange', function() {
   //clears timer when navigating to pages
    window.clearTimeout(interval);
    console.log('#Hash change detected & Timer Cleared!!!');
  }
}
// Call again to create a loop - update page every 5 seconds
window.setTimeout(updatePage, 200);


function getUpdates(){
 var URL = 'https://example.com';       
  $.ajax({
      method: 'GET',
      async: true,
      url : URL, // Your url, for eg: https://example.com
      dataType : 'json', // if format of the file type is in json
      data : 'dataObj',
      success : onGetObjSuccess,
      error: function(jqxhr, textStatus, errorThrown) {
    }
  });
}


function onGetObjSuccess(data) {
  //Do something here to refresh and set data in html
  // This replaces old data with new one dynamically.
  $('#yourDiv_id').replaceWith(data);
  console.log(data);
}
</script>

函数updatePage(){
//加载页面后立即调用Ajax函数
setTimeout(getUpdates,200);
var interval=window.setTimeout(updatePage,5000);
//在此处使用更新函数^
//yourUpdateScript();
addEventListener('hashchange',function(){
//导航到页面时清除计时器
窗口清除超时(间隔);
console.log(“#检测到哈希更改并清除计时器!!!”);
}
}
//再次调用以每5秒创建一个循环更新页面
setTimeout(updatePage,200);
函数getUpdates(){
var URL='1〕https://example.com';       
$.ajax({
方法:“GET”,
async:true,
url:url,//您的url,例如:https://example.com
dataType:'json',//如果文件类型的格式为json
数据:“dataObj”,
成功:onGetObjSuccess,
错误:函数(jqxhr、textStatus、errorshown){
}
});
}
函数onGetObjSuccess(数据){
//在此处执行一些操作以刷新和设置html中的数据
//这将动态地用新数据替换旧数据。
$('yourDiv_id')。替换为(数据);
控制台日志(数据);
}

@Nitro.de附加到
文档中
的作用很小sense@charlietfl你当然是对的。我们没有足够的代码给OP一个很好的例子,我想。@Nitro.de这并不意味着提供毫无用处的代码对任何人都有帮助hanks@david,它工作起来很有魅力,很抱歉没有及时回复