如何修复axios中的'TypeError:name.toUpperCase不是一个函数'

时间:2019-06-05 12:37:44

标签: vue.js axios dbpedia

我想通过ue js中的axios从dbpedia端点获取数据。

我使用axios.get从dbpedia中获取数据,但在控制台中却出现错误提示:

  

TypeError:name.toUpperCase不是函数

我该如何解决?

created(){
    this.fetch();
  },
  methods: {
    fetch(){
      axios
      .get('http://id.dbpedia.org/sparql?query=SELECT+DISTINCT+?concept+WHERE+{+?s+a+?concept+}+LIMIT+50', {
        headers: 'Access-Control-Allow-Origin: *'
      }).then(response => {
        /* eslint-disable */
        console.log('SUCCESS');
        console.log(response);
      }).catch((e) => {
        console.log(e);
      })
    }
  },

3 个答案:

答案 0 :(得分:0)

我在学习阶段遇到了类似的问题,并通过将标头作为对象而不是字符串来解决了问题:

headers: { 'Access-Control-Allow-Origin': true }

答案 1 :(得分:0)

您需要将Axios请求更改为此:

methods: {
async fetch () {
await axios.get('https://cors.io/?http://id.dbpedia.org/sparql?query=SELECT+DISTINCT+?concept+WHERE+{+?s+a+?concept+}+LIMIT+50', {
            headers: {'Access-Control-Allow-Origin': *},
             mode: 'cors',
          }).then(response => {
            /* eslint-disable */
            console.log('SUCCESS');
            console.log(response.data);
          }).catch((e) => {
            console.log(e);
          }
     }
}

五个变化:

1)将Axios headers做成一个对象(注意引号在哪里)

2)console.log(response.data)

3)添加了mode: 'cors'

4)为网址添加了cors前缀,因为您是从托管环境之外的第三方域中检索数据

5)由于axios是基于Promise的库,因此将fetch函数包装在async await中。

答案 2 :(得分:0)

至以上答案 先决条件是您允许您出身。要指定我在下面有一个例子

app.use(cors({credentials: true, origin: 'http://localhost:3000'}));