简体   繁体   English

从前端和 CORS 获取数据

[英]Getting Data from the Frontend & CORS

i´m stuck at a little demo project from udemy with django and api calls.我被困在来自 udemy 的一个带有 django 和 api 调用的小演示项目中。 I have this in main.js and I want to get array of object and not just a json list in console.我在 main.js 中有这个,我想获取对象数组,而不仅仅是控制台中的 json 列表。

let projectsUrl = 'http://127.0.0.1:8000/api/projects/'

let getProjects = () => {
   
    fetch(projectsUrl)
        .then(response => response.json())
        .then(data => {
            console.log(data)
            buildProjects(data)
        })
   
}

let buildProjects = (projects) => {
    
    let projectsWrapper = document.getElementById('testz')

    for (let i = 0; projects.lenght > i; i++){
        let project = projects[i]
        console.log(project)
    }
}


getProjects()

The result :结果 :

Console Log控制台日志

and the result should be something like this :结果应该是这样的:

Console Log控制台日志

There is a typo.有一个错字。

change改变

projects.lenght

to

projects.length

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

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