How to fix 'TypeError: name.toUpperCase is not a function' in axios
How to fix 'TypeError: name.toUpperCase is not a function' in axios
我想从 dbpedia 端点获取数据,在 vue js 中使用 axios。
我使用 axios.get
从 dbpedia 获取数据,我在控制台中收到错误消息:
TypeError: name.toUpperCase is not a function
我该如何解决?
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);
})
}
},
我在学习阶段遇到过类似的问题,通过将 headers 设为 object 而不是字符串来解决:
headers: { 'Access-Control-Allow-Origin': true }
您需要将 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) 向 URL 添加了 cors
前缀,因为您要从托管环境 third-party 域之外的域检索数据
5) 将您的 fetch
函数包装在 async
await
中,因为 axios 是一个 promise-based 库。
对上面的回答
前提是你允许你的出身。指定我下面有一个例子
app.use(cors({credentials: true, origin: 'http://localhost:3000'}));
我想从 dbpedia 端点获取数据,在 vue js 中使用 axios。
我使用 axios.get
从 dbpedia 获取数据,我在控制台中收到错误消息:
TypeError: name.toUpperCase is not a function
我该如何解决?
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);
})
}
},
我在学习阶段遇到过类似的问题,通过将 headers 设为 object 而不是字符串来解决:
headers: { 'Access-Control-Allow-Origin': true }
您需要将 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) 向 URL 添加了 cors
前缀,因为您要从托管环境 third-party 域之外的域检索数据
5) 将您的 fetch
函数包装在 async
await
中,因为 axios 是一个 promise-based 库。
对上面的回答 前提是你允许你的出身。指定我下面有一个例子
app.use(cors({credentials: true, origin: 'http://localhost:3000'}));