ajax 调用后网页无响应
Web page becomes unresponsive after ajax call
我有一个网页,其中我从 server.I 获取一些数据已经使用 servlet 从 server.I 获取数据需要每 5 秒获取一次数据 inerval.I 在我的脚本中使用了 ajax 调用,但是在几次调用之后网页变成了 unresponsive.I 发现了一个东西,我在这里再次替换了整个 html 页面,我怎么能分开一个特定的div从输出html内容(这里是page_html)。我只想替换 div
setInterval("update_content();", 5000);
function update_content(){
$.ajax({
url: "url", // post it back to itself - use relative path or consistent www. or non-www. to avoid cross domain security issues
type: "POST",
async: true,
cache: false, // be sure not to cache results
})
.done(function( page_html ) {
var newDoc = document.open("text/html", "replace");
newDoc.write(page_html);
newDoc.close();
});
}
how can I seperate a particular div from the output html content (here page_html). I want to replace the div only
您可以使用 jQuery html()
方法简单地更改 div
的内容。
<div id="yourDivId">
</div>
JS:
function update_content(){
$.ajax({
url: "url", // post it back to itself - use relative path or consistent www. or non-www. to avoid cross domain security issues
type: "POST",
async: true,
cache: false, // be sure not to cache results
})
.done(function(page_html) {
$("#yourDivId").html(page_html);
});
}
此方法将使用 AJAX 加载 HTML,然后用加载的数据替换 #yourDivId
的 HTML 内容。这就是 AJAX 通常使用的。
我有一个网页,其中我从 server.I 获取一些数据已经使用 servlet 从 server.I 获取数据需要每 5 秒获取一次数据 inerval.I 在我的脚本中使用了 ajax 调用,但是在几次调用之后网页变成了 unresponsive.I 发现了一个东西,我在这里再次替换了整个 html 页面,我怎么能分开一个特定的div从输出html内容(这里是page_html)。我只想替换 div
setInterval("update_content();", 5000);
function update_content(){
$.ajax({
url: "url", // post it back to itself - use relative path or consistent www. or non-www. to avoid cross domain security issues
type: "POST",
async: true,
cache: false, // be sure not to cache results
})
.done(function( page_html ) {
var newDoc = document.open("text/html", "replace");
newDoc.write(page_html);
newDoc.close();
});
}
how can I seperate a particular div from the output html content (here page_html). I want to replace the div only
您可以使用 jQuery html()
方法简单地更改 div
的内容。
<div id="yourDivId">
</div>
JS:
function update_content(){
$.ajax({
url: "url", // post it back to itself - use relative path or consistent www. or non-www. to avoid cross domain security issues
type: "POST",
async: true,
cache: false, // be sure not to cache results
})
.done(function(page_html) {
$("#yourDivId").html(page_html);
});
}
此方法将使用 AJAX 加载 HTML,然后用加载的数据替换 #yourDivId
的 HTML 内容。这就是 AJAX 通常使用的。