Javascript 从API访问JSON信息
我有此api请求:Javascript 从API访问JSON信息,javascript,reactjs,axios,Javascript,Reactjs,Axios,我有此api请求: module.exports = { getResult: function (topic) { return axios.get('https://www.googleapis.com/books/v1/volumes?q=Lord+of+the+rings&callback=handleResponse') .then(function (results) { return resul
module.exports = {
getResult: function (topic) {
return axios.get('https://www.googleapis.com/books/v1/volumes?q=Lord+of+the+rings&callback=handleResponse')
.then(function (results) {
return results.data;
})
}
}
我无法访问内部的数组,因为我在API JSON数据中有此HandlerResponse您可以使用此url。放下&callback=handleResponse
或者,如果希望保留现有url,则需要添加一个全局handleResponse函数,如
<script>
function handleResponse(response) {
for (var i = 0; i < response.items.length; i++) {
var item = response.items[i];
// in production code, item.text should have the HTML entities escaped.
document.getElementById("content").innerHTML += "<br>" + item.volumeInfo.title;
}
}
</script>
功能句柄响应(响应){
对于(var i=0;i”+item.volumeInfo.title;
}
}
但是适合您的用例请显示您调用的代码
getResult
您的响应格式不正确。您需要以json格式发送它。您可以控制台吗?注销结果并向我们显示,您还可以检查浏览器控制台是否有任何错误吗?作为结果。数据,您会得到什么?阵列?一根绳子?