xmlhttprequest - 未捕获的 TypeError:datapoints.data.map 不是 XMLHttpRequest.xmlhttp.onreadystatechange 处的函数
问题描述
谁能帮我?我真的不擅长编程
错误:未捕获的类型错误:datapoints.data.map 不是 XMLHttpRequest.xmlhttp.onreadystatechange 处的函数
const xmlhttp = new XMLHttpRequest();
const url = '//url//';
xmlhttp.open('GET', url, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
const datapoints = JSON.parse(this.responseText);
//console.log(datapoints);
const labelsboy = datapoints.data.map(function(item){
return item.boy;
});
console.log(labelsboy);
}
}
文件 JSON API
{“状态”:真,“行”:2,“数据”:{“男孩”:10,“女孩”:15 } }
解决方案
map
is an array function but datapoints.data
is an object. It seems like you are just trying to get one value from the object so you can just access it directly.
const labelsboy = datapoints.data.boy;
console.log(labelsboy);
推荐阅读
- php - PHP 打印 - 如何在本地服务器打印机上直接从 PHP 打印?
- c++ - 如何正确地将 std::string 转换为整数向量
- javascript - 如何在 JestJS 中模拟带有回调参数的函数
- java - Spring Boot @RequestBody 将空值绑定到默认值?
- python - 基于不同列的多个过滤器聚合pandas df中列的值 - python2.7
- c - 在 Windows 中使用 SendInput() 突出显示文本
- javascript - Javascript原型继承和“instanceof”
- django - Django REST Framework:尝试使用嵌套序列化程序更新时出现类型错误
- javascript - 将 props 直接传递给实用程序函数
- javascript - Discord BOT,guildMemberSpeaking 事件不起作用