简体   繁体   English

如何将变量从ajax成功发送到模板?

[英]How to send a variable from ajax success to a template?

Is it possible reload a page without refresh (silently) ? 是否可以重新加载页面而不刷新(无提示)? Here is an example where page reload automatically. 是自动重新加载页面的示例。 But you can easily see that the page is refreshing after certain time interval. 但是您可以很容易地看到在一定时间间隔后页面正在刷新。 I don't like this where end user is getting such experience. 我不喜欢在最终用户获得这种体验的地方。 I want that the page reload but without refreshing. 我希望页面重新加载但不刷新。 End user should not feel that page is refreshing. 最终用户不应感到页面正在刷新。 Is it possible using simple html? 是否可以使用简单的html? or jquery? 还是jQuery?

I don't want <META HTTP-EQUIV="refresh" CONTENT="5"> or setTimeout of jquery because it refreshes the page while reloading? 我不希望<META HTTP-EQUIV="refresh" CONTENT="5">或jquery的setTimeout ,因为它会在重新加载时刷新页面吗?

You must perform Ajax requests at set intervals. 您必须按设置的时间间隔执行Ajax请求。 This topic is discussed here: How to fire AJAX request Periodically? 这里讨论该主题: 如何定期触发AJAX请求?

Don't reload the whole page. 不要重新加载整个页面。 Just use ajax to reload the components that needs to be updated. 只需使用ajax重新加载需要更新的组件。 jQuery is perfect for that. jQuery就是完美的选择。

Use a setInterval with an ajax request. 将setInterval与ajax请求一起使用。 I'll update in 1 min with the script. 我将在1分钟内使用脚本进行更新。

var divid = 'yourDivsId'

function createRequestObject() 
{
  var obj;
  var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
    obj = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    obj = new XMLHttpRequest();
  }
  return obj;
}

function sendReq(req) 
{   
  http.open('get', req);
  http.onreadystatechange = handleResponse;
  http.send(null);
}

function handleResponse() 
{   
  if (http.readyState == 4)
  {
    var response = http.responseText;
    document.getElementById(divid).innerHTML=response;
  }
}


var http = createRequestObject();
setInterval('yourUrl', 30000); //or whatever time you want.

Should do the trick. 应该做到的。

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

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