简体   繁体   English

使用Fetch和Cors时,如何从JSON API获取属性?

[英]How to get properties from json API when using fetch, cors?

I need to get properties by accessing this link. 我需要通过访问此链接来获取属性。

        fetch("https://pokeapi.co/api/v2/pokemon/28/").then(results => {
           console.log(results);
        })

I receive this respond in console. 我在控制台中收到此回复。

Response {type: "cors", url: " https://pokeapi.co/api/v2/pokemon/28/ ", redirected: false, status: 200, ok: true, …} body:(...) bodyUsed:true headers:Headers {} ok:true redirected:false status:200 statusText:"" type:"cors" url:" https://pokeapi.co/api/v2/pokemon/28/ " proto :Response 响应{类型:“ cors”,网址:“ https://pokeapi.co/api/v2/pokemon/28/ ”,重定向:false,状态:200,确定:true,…}正文:(...) bodyUsed:true标头:Headers {} ok:true重定向:false status:200 statusText:“”类型:“ cors” url:“ https://pokeapi.co/api/v2/pokemon/28/proto :Response

But I expect to see something like this: 但我希望看到这样的事情:

            {   "forms": [      {           "url":
            > "https://pokeapi.co/api/v2/pokemon-form/28/",             "name": "sandslash"
            >       }   ],  "abilities": [      {           "slot": 3,          "is_hidden": true,
            >           "ability": {
            >               "url":  "name": "defense"   

                 ...

And so on...how can I get this data? 依此类推...如何获取此数据? I can access it from their website pokeapi.co, but not from my app. 我可以从他们的网站pokeapi.co上访问它,但不能从我的应用程序中访问它。 What I am doing wrong? 我做错了什么? Please, help me to understand!! 请帮我理解!!

    fetch("https://pokeapi.co/api/v2/pokemon/28/").then(results => { return results.json();
    }).then(data => {
        console.log(data);
    })

This is a solution. 这是一个解决方案。 It's strange, because few hours ago it was a different response. 这很奇怪,因为几个小时前的反应是不同的。

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

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