I am using the jQuery AJAX to call JSON service. I am then spitting out the data to .html. I would like 2 things to happen.
1. I want a refresh button to refresh the data, not the whole page.
2. I want a setTimeout or setInterval (which ever works best) to update the data every 5 minutes or so. But refresh the page.
How would I wrap the AJAX in a setTimeout or setInterval or refresh the data using a button and a timer every 5 minutes or so. I know this should be simple but I have not been able to get it to work. Thanks in advance.
Below is my code.
$.ajax({
type: "POST",
url: "/myservice.asmx/WebMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var myval = msg.d;
// $('#jsonstring').html(myval);
var obj = jQuery.parseJSON(myval);
$('#Data1').html(obj.DataOne);
$('#Data2').html(obj.DataTwo);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(xhr.responseText);
alert(xhr.status);
alert(thrownError);
}
});
这篇博客探讨如何利用jQuery的AJAX方法来调用JSON服务,并将数据动态显示到HTML页面上。作者希望实现两个功能:一是通过按钮实现数据的刷新,而不是整个页面的刷新;二是设置定时器,每5分钟自动更新一次数据。目前代码中已经实现了从JSON服务获取数据并填充到页面元素的操作,但尚需添加刷新按钮和定时更新的逻辑。
231

被折叠的 条评论
为什么被折叠?



