简体   繁体   English

如何在 Reactjs 中使用牛津词典 API?

[英]How to use Oxford Dictionary API in Reactjs?

I am a newbie at React.我是 React 的新手。 I am trying to build a dictionary web app.我正在尝试构建字典 web 应用程序。 I want to use the API provided by Oxford dictionary.我想使用牛津词典提供的API。 I have already registered for the prototype version and gathered the API_ID and API_KEY.我已经注册了原型版本并收集了 API_ID 和 API_KEY。 I have tried to use the code provided by the Oxford documentation.我尝试使用牛津文档提供的代码。

import './App.css';
import axios from 'axios';

function App() {

  const app_id = "******"; 
  const app_key = "****************";
  const wordId = "ace";
  const fields = "pronunciations";
  const strictMatch = "false";

  const options = {
    host: 'od-api.oxforddictionaries.com',
    port: '443',
    path: '/api/v2/entries/en-gb/' + wordId + '?fields=' + fields + '&strictMatch=' + strictMatch,
    method: "GET",
    headers: {
      'app_id': app_id,
      'app_key': app_key
    }
  };

  axios.get(options, (resp) => {
    let body = '';
    resp.on('data', (d) => {
      body += d;
    });
    resp.on('end', () => {
      let parsed = JSON.stringify(body);
      console.log(parsed);
    });
  });

  return <div>Hello world!</div>
}

export default App;

I am getting the following errors in the console when I run the app.运行应用程序时,我在控制台中收到以下错误。

有错误的控制台

I am not sure what the errors mean.我不确定这些错误是什么意思。 I want to get an object as a result in the console.我想在控制台中得到一个 object 作为结果。

Someone kindly help me with this.有人请帮我解决这个问题。 If there is any more info required from my side, do ask.如果我这边需要更多信息,请询问。 Thanks in advance!提前致谢!

axios.get({
  method: "get",
  url: 'od-api.oxforddictionaries.com/api/v2/entries/en-gb/' + wordId + '?fields=' + fields + '&strictMatch=' + strictMatch,,
  headers: {
  'app_id': app_id,
  'app_key': app_key
  }
})
.then(response => { // handle successful response})
.catch(error => { // handle error response})

You are using axios the wrong way.您以错误的方式使用axios The code above should work.上面的代码应该可以工作。

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

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