javascript - 无法访问数组元素。将一个 ajax 请求的结果用于另一个 ajax 请求

标签 javascript arrays ajax asynchronous api-eveonline

    //global variables
var corpArray = new Array(); //store corp classes instances
var corpId = new Array(); //store corp id's

window.onload = init();

function init() {
  getNpcCorpId();
  console.log(corpId);
  getCorpNames(corpId[5]);
}

//get all corporation id's from the game
function getNpcCorpId() {
  let conName = new XMLHttpRequest();
  conName.onload = function() {
    if (this.status == 200) {
      let idList = JSON.parse(this.responseText);
      idList.forEach(element => {
        corpId.push(element);
      });
    }
  };
  conName.open(
    "get",
    "https://esi.evetech.net/latest/corporations/npccorps/?datasource=tranquility",
    true
  );
  conName.send();
}

//get corporation name
function getCorpNames(element) {
  console.log(element);
  let corpConn = new XMLHttpRequest();
  corpConn.onload = () => {
    if (this.status == 200) {
      console.log(this.responseText);
    }
  };
  corpConn.open(
    "get",
    `https://esi.evetech.net/latest/corporations/${element}/?datasource=tranquility`,
    true
  );
  corpConn.send();
}

我正在尝试创建一个 eve 在线 api,我想使用 2 个全局变量来存储我检索到的值(因为我不知道另一种方式)我将使用几个函数来使用提供的 eve api。我可以无法访问我的 corpId 单个元素,当我控制台记录我的所有数组时,一切正常,但是当我想访问单个元素时,它似乎不安全。

最佳答案

 //global variables
var corpArray = new Array(); //store corp classes instances
var corpId = new Array(); //store corp id's

window.onload = init();

async function  init() {
  await getNpcCorpId();
  console.log(corpId);
  getCorpNames(corpId[5]);            // asynchronous behaviour - let it be fix using await and async
}

//get all corporation id's from the game
function getNpcCorpId() {
	return new Promise(function(resolve, reject) {
		let conName = new XMLHttpRequest();
		conName.onload = function() {
			if (this.status == 200) {
			  let idList = JSON.parse(this.responseText);
			  idList.forEach(element => {
				corpId.push(element);
			  });
			}
			resolve();
		};
  conName.open(
    "get",
    "https://esi.evetech.net/latest/corporations/npccorps/?datasource=tranquility",
    true
  );
  conName.send();
	
});

  
}

//get corporation name
function getCorpNames(element) {
  console.log(element);
  let corpConn = new XMLHttpRequest();
  corpConn.onload = () => {
    if (this.status == 200) {
      console.log(this.responseText);
    }
  };
  corpConn.open(
    "get",
    `https://esi.evetech.net/latest/corporations/${element}/?datasource=tranquility`,
    true
  );
  corpConn.send();
}

This is due to asynchronous behaviour - in simple terms - when you call getNpcCorpId() it is a http request it will take some time to execute but next line run immediately so at that point of time corpId is still blank. So to fix this issue you can use await that will makes JavaScript wait until the promise returns a result.

希望对你有帮助!

关于javascript - 无法访问数组元素。将一个 ajax 请求的结果用于另一个 ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904430/

相关文章:

javascript - 使用startsWith函数识别邮政编码的前三位数字

javascript - 如何使用 Javascript 隐藏文本框焦点上的文本

arrays - 如何在 HLSL 中使用巨大的数组(错误 X4505)

javascript - python 中的热图

ajax函数内的Javascript变量范围

javascript - rxjs 运行一些操作并返回不同的类型

c# - 读取图像文件元数据

JavaScript - "Filter"通过对象 - 最佳实践?

perl - 如何遍历Perl数组引用?

ajax - 名为 "file"的 Linux 命令仅在通过 Web 执行时返回 "application/x-empty"