简体   繁体   English

jQuery AJAX函数被多次调用但数据仅在第一次调用时被提取

[英]jQuery AJAX function called multiple times but data is only pulled on first call

I have a jQuery function for pulling data from an XML file on a flask server. 我有一个jQuery函数,用于从烧瓶服务器上的XML文件中提取数据。 I start out my ready function with calling this function and all of my variables update with no issues. 我通过调用此函数开始我的ready函数,并且所有变量都没有问题地更新。 However, when I call the function a second time from a click action my variables do not update. 但是,当我从单击操作第二次调用该函数时,我的变量不会更新。 I know that the AJAX function is being called successfully because I get my alerts. 我知道AJAX函数被成功调用,因为我得到了警报。 It seems as thought the XML document is not being re-loaded after the first call. 似乎第一次调用后没有重新加载XML文档。 How can I get my variables to update on every call to the function. 如何在每次调用函数时更新变量。

Global Variables 全局变量

//Variables representing Sensor Data
var bspeed=0;
var set_temp=0;
var f_temp=0;
var fuel_level=0;
var s_temp=0;
var humidity=0;
var bspeed_data=0;
var set_temp=0;
var mode = "mod";
var mod_mode = "blower";

Data-Pull function 数据拉动功能

//AJAX XML file parsing
function pull_data(){

//Gather Data from main.xml and store into variables
$.ajax({
    type: "GET",
    url: "static/main.xml",
    async: false,
    success: function(xml){
        $(xml).find('item').each(function(){
            var id = $(this).find('id').text();
            switch(id){
                case "ftemp_data":
                    f_temp = $(this).find('value').text();
                    break;
                case "stemp_data":
                    s_temp = $(this).find('value').text();
                    break;
                case "fuel_level_data":
                    fuel_level = $(this).find('value').text();
                    break;
                case "humidity_data":
                    humidity = $(this).find('value').text();
                    break;
                case "blower_speed":
                    alert("Changing bspeed");
                    bspeed_data = $(this).find('value').text();
            }
        });
        alert("Pull Success "+ bspeed_data);
    },
    error: function(data){
        alert("AJAX failed: "+ data.status + ' ' + data.statusText);
    }
});

//Populate HTML with variables
$("#ftemp_data").text(f_temp+("\u00B0"));
$("#stemp_data").text(s_temp+("\u00B0"));
$("#fuel_level_data").text(fuel_level+"%");
$("#humidity_data").text(humidity+"%");
$("#bspeed_data").text(bspeed_data);
}

Ready Function (Simplified) 就绪功能(简化)

$(document).ready(function(){
    pull_data();
    //Click Refresh Button
    $("#refresh_btn").click(function(){
        pull_data();
    }); 
}

I'm fairly new to web development so feel free to point out structural issues with what I'm doing. 我对Web开发还很陌生,所以请随意指出我正在做的结构问题。

Probably you need to force to refresh the cache, in order to do this, add a property cache: false in the ajax code: 可能你需要强制刷新缓存,为了做到这一点,在ajax代码中添加属性cache:false:

$.ajax({
    type: "GET",
    url: "static/main.xml",
    async: false,
    cache: false,...

You can check in Jquery Ajax documentation 您可以签入Jquery Ajax文档

I think you are getting the cached response, you need to add timestamp to the url , 我想你正在获得缓存的响应,你需要在url中添加时间戳,

var date = new Date();
var timestamp = date.getTime();
$.ajax({
  type: "GET",
  url: "static/main.xml?="+timestamp
})

It looks like you're updating the values of the variables on a successful request callback, however your code to update the text representation is being called outside the AJAX function. 看起来你正在成功的请求回调中更新变量的值,但是更新文本表示的代码是在AJAX函数之外调用的。 This means that when the function is called, the AJAX request is made and the DOM is being updated before the request has finished. 这意味着在调用函数时,会发出AJAX请求,并在请求完成之前更新DOM。 You need to move your DOM update logic into the success handler of $.ajax after updating the variables. 更新变量后,需要将DOM更新逻辑移动到$ .ajax的成功处理程序中。

It appears you're getting a cached response. 看来你正在获得一个缓存的响应。 You should use cache: false in your request options, but I would also recommend removing the async: false option as well since you're using callbacks to update the variables' values anyway. 您应该在请求选项中使用cache: false ,但我还建议您删除async: false选项,因为您正在使用回调来更新变量的值。 Here's the updated code: 这是更新的代码:

//AJAX XML file parsing
function pull_data(){

//Gather Data from main.xml and store into variables
$.ajax({
    type: "GET",
    url: "static/main.xml",
    async: false,
    success: function(xml){
        $(xml).find('item').each(function(){
            var id = $(this).find('id').text();
            switch(id){
                case "ftemp_data":
                    f_temp = $(this).find('value').text();
                    break;
                case "stemp_data":
                    s_temp = $(this).find('value').text();
                    break;
                case "fuel_level_data":
                    fuel_level = $(this).find('value').text();
                    break;
                case "humidity_data":
                    humidity = $(this).find('value').text();
                    break;
                case "blower_speed":
                    alert("Changing bspeed");
                    bspeed_data = $(this).find('value').text();
            }
        });
        alert("Pull Success "+ bspeed_data);

        //Request was successful, populate HTML with variables
        $("#ftemp_data").text(f_temp+("\u00B0"));
        $("#stemp_data").text(s_temp+("\u00B0"));
        $("#fuel_level_data").text(fuel_level+"%");
        $("#humidity_data").text(humidity+"%");
        $("#bspeed_data").text(bspeed_data);
    },
    error: function(data){
        alert("AJAX failed: "+ data.status + ' ' + data.statusText);
    }
});
}

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

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