How do I get a random word using the Wordnik API in Javascript?

I am trying to create an executioner game using HTML and Javascript, and in order to get a word, I was wondering how I can get a random word using the dictionary API.

I don't understand how to get the word out and then return. I have already signed up for apiKey, but I get confused all the time about how to make AJAX and JSON part of the API and how it fits with Javascript.

+3


source to share


2 answers


According to a quick document search, you will be able to get a list of random words via:

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

      

You are actually looking for a list of random words with a one word limit ( limit=1

).



Obviously, use your own api_key

, not the demo key listed in the documentation.

Literature:

+7


source


Seems late, but now everyone uses React for everything, so you can try something like this:

<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>
      

Run codeHide result


0


source







All Articles