Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过GET不重复地更新对象值_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 通过GET不重复地更新对象值

Javascript 通过GET不重复地更新对象值,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在处理一个平面表,它有一个包含多个对象的脚本,如下所示: var plano_basicoUS = { 1: null, // monthly 2: null, // quarterly 3: null, // semester 4: null, // yearly }; var plano_economicoUS = { 1: null, // monthly 2: null, // quarterly 3: null, //

我正在处理一个平面表,它有一个包含多个对象的脚本,如下所示:

var plano_basicoUS = { 
    1: null, // monthly
    2: null, // quarterly
    3: null, // semester
    4: null, // yearly
};

var plano_economicoUS = { 
    1: null, // monthly
    2: null, // quarterly
    3: null, // semester
    4: null, // yearly
};
$.get("buscar_valor.php?id=3&periodicidade=monthly", function(resultado){
    plano_basicoUS[1] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=3&periodicidade=quarterly", function(resultado){
    plano_basicoUS[2] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=3&periodicidade=semiannually", function(resultado){
    plano_basicoUS[3] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=3&periodicidade=annually", function(resultado){
    plano_basicoUS[4] = parseFloat(resultado);
});

$.get("buscar_valor.php?id=4&periodicidade=monthly", function(resultado){
    plano_economicoUS[1] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=4&periodicidade=quarterly", function(resultado){
    plano_economicoUS[2] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=4&periodicidade=semiannually", function(resultado){
    plano_economicoUS[3] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=4&periodicidade=annually", function(resultado){
    plano_economicoUS[4] = parseFloat(resultado);
});
我通过
$更新每个的值。获取
,如下所示:

var plano_basicoUS = { 
    1: null, // monthly
    2: null, // quarterly
    3: null, // semester
    4: null, // yearly
};

var plano_economicoUS = { 
    1: null, // monthly
    2: null, // quarterly
    3: null, // semester
    4: null, // yearly
};
$.get("buscar_valor.php?id=3&periodicidade=monthly", function(resultado){
    plano_basicoUS[1] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=3&periodicidade=quarterly", function(resultado){
    plano_basicoUS[2] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=3&periodicidade=semiannually", function(resultado){
    plano_basicoUS[3] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=3&periodicidade=annually", function(resultado){
    plano_basicoUS[4] = parseFloat(resultado);
});

$.get("buscar_valor.php?id=4&periodicidade=monthly", function(resultado){
    plano_economicoUS[1] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=4&periodicidade=quarterly", function(resultado){
    plano_economicoUS[2] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=4&periodicidade=semiannually", function(resultado){
    plano_economicoUS[3] = parseFloat(resultado);
});
$.get("buscar_valor.php?id=4&periodicidade=annually", function(resultado){
    plano_economicoUS[4] = parseFloat(resultado);
});
$.get
的每个查询都会通过PHP文件返回一个值,例如:10.00

由于有多个对象,我不想对每个对象重复GET


注意到每个
$.get
都有不同的URL,我如何动态地执行此操作?或者按顺序,如果是这样的话。

理想情况下,您应该避免为每个数据发送一个调用。与此相反,您应该发送一个调用并获取所有数据,然后在对象中填充信息

如果这是不可能的,那么你可以选择


您可以编写一个包含公共部分的函数。比如:

const timescales = ["monthly","quarterly","semester","yearly"];

function getNumberByTimescale(timescale, index) {
  $.get("buscar_valor.php?id=4&periodicidade=" + timescale, function(resultado){
    plano_economicoUS[index] = parseFloat(resultado);
  });
}

for (var i = 0; i < timescales.length; i++) {
  getNumberByTimescale(timescales[i], i + 1); // Adding one to match timescales array index with your arrays' keys.
}
const timescales=[“每月”、“每季度”、“每学期”、“每年”];
函数getNumberByTimescale(时间刻度,索引){
$.get(“buscar_valor.php?id=4&periodicIDAE=“+时间刻度,函数(resultado)){
经济计划[指数]=parseFloat(resultado);
});
}
对于(变量i=0;i

这只是一种方法,但重要的部分是将重复的工作抽象为函数。

您应该通过一次调用返回
JSON
,其中包含来自后端的所有更新值,这不是因为我缺乏知识,而是因为我不完全理解您的意思。现在,您将发送8个不同的请求来执行琐碎(且高度相关)的数据检索。相反,使用1个调用执行所有数据检索更为合理,该调用返回
JSON
,包含所有您查找的值。问题是,我已经创建了一个迷你API,通过GET执行这些查询,因此对于每个周期,它将只返回每个计划的单个值。您是否知道并可以在此处公开其他替代方案?