简体   繁体   English

未捕获的类型错误:对象在 XMLHttpRequest 中不可迭代(无法读取属性 Symbol(Symbol.iterator))。<anonymous>

[英]Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) at XMLHttpRequest.<anonymous>

const container = document.querySelector("#container");

request.open("GET","https://datausa.io/api/data?drilldowns=Nation&measures=Population") ; 


request.send();

// to get the response


request.addEventListener("load",function(){

    //  console.log( this.responseText);

     const [data]= JSON.parse(this.responseText);
     console.log(data);

    //  const htmlData ="<div></div>";
    // container.insertAdjacentHTML("afterbegin",htmlData)
})

I am getting error in JSON.parse(this.responseText) the above mentioned error is for this line,please help me solve it我在 JSON.parse(this.responseText) 中遇到错误,上面提到的错误是针对这一行的,请帮我解决它

You don't need [] or {} surrounding data .您不需要 [] 或 {} 环绕数据 Just data is enough in this case.在这种情况下,只有数据就足够了。

const data = JSON.parse(this.responseText);

The call returns this:调用返回:

{"data":[{"ID Nation":"01000US","Nation":"United States","ID Year":2019,"Year":"2019","Population":328239523,"Slug Nation":"united-states"},{"ID Nation":"01000US","Nation":"United States","ID Year":2018,"Year":"2018","Population":327167439,"Slug Nation":"united-states"},{"ID Nation":"01000US","Nation":"United States","ID Year":2017,"Year":"2017","Population":325719178,"Slug Nation":"united-states"},{"ID Nation":"01000US","Nation":"United States","ID Year":2016,"Year":"2016","Population":323127515,"Slug Nation":"united-states"},{"ID Nation":"01000US","Nation":"United States","ID Year":2015,"Year":"2015","Population":321418821,"Slug Nation":"united-states"},{"ID Nation":"01000US","Nation":"United States","ID Year":2014,"Year":"2014","Population":318857056,"Slug Nation":"united-states"},{"ID Nation":"01000US","Nation":"United States","ID Year":2013,"Year":"2013","Population":316128839,"Slug Nation":"united-states"}],"source":[{"measures":["Population"],"annotations":{"source_name":"Census Bureau","source_description":"The American Community Survey (ACS) is conducted by the US Census and sent to a portion of the population every year.","dataset_name":"ACS 1-year Estimate","dataset_link":"http://www.census.gov/programs-surveys/acs/","table_id":"B01003","topic":"Diversity","subtopic":"Demographics"},"name":"acs_yg_total_population_1","substitutions":[]}]} 

And as you can see this is an object not an array, so to get data key from the object you need to use curly brackets instead of normal brackets.正如您所看到的,这是一个对象而不是数组,因此要从对象中获取数据键,您需要使用大括号而不是普通括号。

Like this:像这样:

const {data} = JSON.parse(this.responseText);

暂无
暂无

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

相关问题 × TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - × TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) 未捕获的 TypeError:object 在 handleInput 处不可迭代(无法读取属性 Symbol(Symbol.iterator)) - Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) at handleInput Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) 不知道该怎么做 - Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) Not sure what to do TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - 为什么? 这是什么意思? 如何解决? - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - Why? What does it mean? How to solve it? 使用 React Context API 时出错:“TypeError:Object 不可迭代(无法读取属性 Symbol(Symbol.iterator))” - Error while using React Context API: "TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))" React context throwing TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) - React context throwing TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) 篮子 - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) basket TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) JavaScript - TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) JavaScript 如何修复:React-Context - TypeError: Object 不可迭代(无法读取属性 Symbol(Symbol.iterator)) - How to fix: React-Context - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM