首页 > 解决方案 > 未捕获的 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 } }

标签: xmlhttprequesttypeerror

解决方案


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);

推荐阅读