JavaScript Axios 获取数据= null
JavaScript Axios GET data= null
我可能做错了什么,但我找不到原因。
我尝试使用 axios 执行 GET,如果我从邮递员那里执行,答案是正确的,但是从 axios 中,它 returns 数据为空。
邮递员:
{
"direccion": "Castelldefels, Spain",
"lat": "41.279999",
"lng": "1.970000"
}
代码JS:
var boton = document.getElementById('location');
boton.addEventListener('click', function() {
loading.style.display = 'block';
axios.get('https://restserver-test-jsg.herokuapp.com/location?location=Castelldefels', {
responseType: 'json',
})
.then(function(res) {
if (res.status == 200) {
console.log(res);
mensaje.innerHTML = res.data;
}
console.log(res);
})
.catch(function(err) {
console.log(err);
})
.then(function() {
loading.style.display = 'none';
});
});
我从 Chrome 控制台看到的答案是:
chrome
如果我从浏览器执行此操作,它 returns 符合预期。
https://restserver-test-jsg.herokuapp.com/location?location=Castelldefels
谢谢!
难道只是axios的语法错误?配置对象中的 responseType 键后可能不应该有逗号。
编辑:
啊,我明白了。它看起来是 heroku 的 CORS 错误。检查这个问题的答案:Allow CORS REST request to a Express/Node.js application on Heroku
我可能做错了什么,但我找不到原因。
我尝试使用 axios 执行 GET,如果我从邮递员那里执行,答案是正确的,但是从 axios 中,它 returns 数据为空。
邮递员:
{
"direccion": "Castelldefels, Spain",
"lat": "41.279999",
"lng": "1.970000"
}
代码JS:
var boton = document.getElementById('location');
boton.addEventListener('click', function() {
loading.style.display = 'block';
axios.get('https://restserver-test-jsg.herokuapp.com/location?location=Castelldefels', {
responseType: 'json',
})
.then(function(res) {
if (res.status == 200) {
console.log(res);
mensaje.innerHTML = res.data;
}
console.log(res);
})
.catch(function(err) {
console.log(err);
})
.then(function() {
loading.style.display = 'none';
});
});
我从 Chrome 控制台看到的答案是: chrome
如果我从浏览器执行此操作,它 returns 符合预期。 https://restserver-test-jsg.herokuapp.com/location?location=Castelldefels
谢谢!
难道只是axios的语法错误?配置对象中的 responseType 键后可能不应该有逗号。
编辑:
啊,我明白了。它看起来是 heroku 的 CORS 错误。检查这个问题的答案:Allow CORS REST request to a Express/Node.js application on Heroku