【问题标题】:How to get random word using Wordnik API in Javascript?如何在 Javascript 中使用 Wordnik API 获取随机单词?
【发布时间】:2014-10-29 04:17:40
【问题描述】:

我正在尝试使用 HTML 和 Javascript 创建一个刽子手游戏并获得单词,我想知道如何使用 wordnik API 获得随机单词。

我不明白如何获取单词然后返回。我已经注册了一个 apiKey,但我一直对如何执行 API 的 AJAX 和 JSON 部分以及如何与 Javascript 结合感到困惑.

【问题讨论】:

  • 这个项目在 Github 上很漂亮well documented。它应该让您朝着正确的方向开始。他们还提供示例代码 sn-ps。

标签: javascript html api wordnik


【解决方案1】:

根据对文档的快速搜索,您应该能够通过以下方式获得随机单词列表:

http://api.wordnik.com:80/v4/words.json/randomWords?hasDictionaryDef=true&minCorpusCount=0&minLength=5&maxLength=15&limit=1&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5

实际上,您正在寻找一个随机单词列表,限制为一个单词 (limit=1)。

显然,请使用您自己的api_key,而不是文档中提供的演示密钥。

参考资料:

【讨论】:

    【解决方案2】:

    有点晚了,但现在每个人都在使用 React,因此你可以尝试这样的事情:

    <div id="root"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
    <script type="text/babel">
    
    const wordnik = 'https://api.wordnik.com/v4/words.json/randomWord?&minLength=5&maxLength=-1&api_key=';
    const API_KEY = '';
    
    class FetchData extends React.Component {
        state = {
          word: '',
        }
    
        componentDidMount() {
          fetch(wordnik + API_KEY)
          .then(res => res.json())
          // Uncomment here if you have API_KEY
          // .then(json => this.setState({ word: json.word }))
    
          // Comment here if you have API_KEY
          .then(json => this.setState({ word: json.message }))
          .catch(err => console.log(err.message));
        }
        render() {
            return <h1>{this.state.word}</h1>;
        }
    }
    
    ReactDOM.render(<FetchData />, document.getElementById('root'));
    </script>
    

    <div id="root"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
    <script type="text/babel">
    const wordnik = 'https://api.wordnik.com/v4/words.json/randomWord?&minLength=5&maxLength=-1&api_key=';
    const API_KEY = '';
    
    class FetchData extends React.Component {
        state = {
          word: '',
        }
    
        componentDidMount() {
          fetch(wordnik + API_KEY)
          .then(res => res.json())
          // Uncomment here if you have API_KEY
          // .then(json => this.setState({ word: json.word }))
          
          // Comment here if you have API_KEY
          .then(json => this.setState({ word: json.message }))
          .catch(err => console.log(err.message));
        }
        render() {
            return <h1>{this.state.word}</h1>;
        }
    }
    
    ReactDOM.render(<FetchData />, document.getElementById('root'));
    </script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多