简体   繁体   English

无法获取API响应的特定元素[JS]

[英]Can't get specific element of API response [JS]

I can't seem to get a specific part out of the response below, using the code that is also below. 使用下面的代码,我似乎无法从下面的响应中得到特定的部分。 I get an "undefined" in the console log when I try to use response["minecraft.net"] . 尝试使用response["minecraft.net"]时,控制台日志中显示“ undefined”。 Can someone explain to me why this isn't working? 有人可以向我解释为什么这行不通吗?

Response: 响应:

"[{"minecraft.net":"green"},{"session.minecraft.net":"green"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"green"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"},{"mojang.com":"green"}]" 

Code: 码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mojang Servives Status</title>
</head>

<body>
<script>
var api = new XMLHttpRequest();
api.open("GET", "https://status.mojang.com/check", false);
api.send();
console.log(api);
var response = JSON.parse(api.response)
console.log(response["minecraft.net"]);
</script>
</body>

</html>

The response is wrapped inside of an array. 响应包装在数组中。 So you'd use response[0]['minecraft.net'] 因此,您将使用response[0]['minecraft.net']

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

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