0

谁能帮助我我做错了什么我无法从中获取对象(API 密钥不在代码中)。关于如何学习 API 的一些建议会很好。我至少想要console.log()JSON 并从那里开始。

import React, { Component } from 'react';
import './App.css';


class App extends Component {
  constructor(props){
    super(props);
    this.state ={
      error: null,
      isLoaded: false,
      items: []
    };
  }

  componentDidMount() {
    fetch("https://eun1.api.riotgames.com/lol/summoner/v3/summoners/by-name/EvilDex?api_key=", {mode: "no-cors"})
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            items: result.items
          })
        },
        (error) =>{
          this.setState({
            isLoaded: true,
            error
          })
        }
      )
    
  }

  

4

1 回答 1

0

您不能使用客户端 Javascript 调用 API。API 不允许这样做,它还会将您的 API 密钥公开给从浏览器检查您的代码的任何人。

这个过程应该是这样的: React Client 向服务器请求数据 -> 服务器向 Riot API 请求数据 -> Riot API 以数据响应 -> 服务器处理数据(格式/排序/过滤或任何你想用它做的事情) -> 用数据响应 React 客户端 -> React 在 JSX 中呈现数据。

于 2018-09-27T21:05:40.540 回答