简体   繁体   English

jquery.Ajax数据获取

[英]jquery.Ajax data obtaining

I try to obtain data stored in another url. 我尝试获取存储在另一个URL中的数据。 There is a text field in which I want to set the value retrieved from the url at every 1 second(The url's content is continually changing at each second). 有一个文本字段,我想在其中设置每1秒从网址中检索到的值(网址的内容在每秒都在不断变化)。 How can I do that? 我怎样才能做到这一点? I don't want refreshing entire page at each second. 我不希望每一秒刷新整个页面 Instead, only the field must be refreshed. 相反,只需要刷新字段。 But, in my code, the field is not set as intended and it is empty. 但是,在我的代码中,字段未按预期设置且为空。

What's wrong with my idea/approach? 我的想法/方法有什么问题?

Html, HTML,

<div class="form-group">
<label class="" for="temperature">Temp.:</label>
<input type="text" id="temperature" class="form-control">
</div>

My ajax/jquery, 我的ajax / jquery,

$(document).ready(function() {
    setInterval(function() {
        $.ajax('/theURLData', {
            success: function(data, status, xhr) {
                $('#temperature').val(data);
            }
        });
    }, 1000);
});

I don't think setInterval(function()… works as intended here. Try the following, 我不认为setInterval(function()…在这里按预期工作。请尝试以下方法,

<script type = "text/javascript"> 
  $(document).ready(function f() {
    $.ajax('/theURLData', {
        success: function(data, status, xhr) {
            $('#temperature').val(data);
            setTimeout(f, 1000);
        }
    });
}); 
</script>

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

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