Javascript 无法填充json响应的数据
我有一个API调用,其响应如下Javascript 无法填充json响应的数据,javascript,node.js,express,Javascript,Node.js,Express,我有一个API调用,其响应如下 { "name": "Cocktails & Dreams, Speakeasy", "url": "https://www.zomato.com/ncr/cocktails-dreams-speakeasy-sector-15-gurgaon?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2
{
"name": "Cocktails & Dreams, Speakeasy",
"url": "https://www.zomato.com/ncr/cocktails-dreams-speakeasy-sector-15-gurgaon?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "SCO 23, Sector 15, Gurgaon",
"locality": "Sector 15",
"city": "Gurgaon",
"city_id": 1,
"latitude": "28.4579754130",
"longitude": "77.0446266979",
"zipcode": "0",
"country_id": 1,
"locality_verbose": "Sector 15, Gurgaon"
},
"price": 4,
"thumbnail": "https://b.zmtcdn.com/data/res_imagery/300716_RESTAURANT_b026340288ddc8287349d90b0b712940.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A",
"rating": "4.8"
}
Ans这是静态文件夹中的my index.js,用于链接前端和后端
searchForm.addEventListener('submit', async e => {
e.preventDefault();
resultArea.innerHTML = '';
const query = e.target.querySelector('#restaurant-name').value;
if (query === '') {
return
}
e.target.querySelector('#restaurant-name').value = '';
const res = await fetch(`${hostname}/locations/${query}`, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: 'POST',
})
const json = await res.json();
populateData(json);
});
function populateData(results) {
results(result => {
const newResult = rTemp.content.cloneNode(true);
newResult.querySelector('.result-title').innerText = result.name;
newResult.querySelector('.result-neighborhood').innerText = result.location.locality;
newResult.querySelector('.result-address').innerText = result.location.address;
newResult.querySelector('.result-price').innerText = '$'.repeat(result.price);
newResult.querySelector('.result-thumbnail').src = result.thumbnail;
newResult.querySelector('.result-website').href = result.url;
resultArea.appendChild(newResult);
});
}
rTemp
=document.querySelector('template');在前端
在这个例子中,我得到了以下错误
index.js:27 Uncaught (in promise) TypeError: results is not a function
正确的方法是什么?而不是
function populateData(results) {
results(result => {
const newResult = rTemp.content.cloneNode(true);
newResult.querySelector('.result-title').innerText = result.name;
newResult.querySelector('.result-neighborhood').innerText = result.location.locality;
newResult.querySelector('.result-address').innerText = result.location.address;
newResult.querySelector('.result-price').innerText = '$'.repeat(result.price);
newResult.querySelector('.result-thumbnail').src = result.thumbnail;
newResult.querySelector('.result-website').href = result.url;
resultArea.appendChild(newResult);
});
}
使用
而不是
function populateData(results) {
results(result => {
const newResult = rTemp.content.cloneNode(true);
newResult.querySelector('.result-title').innerText = result.name;
newResult.querySelector('.result-neighborhood').innerText = result.location.locality;
newResult.querySelector('.result-address').innerText = result.location.address;
newResult.querySelector('.result-price').innerText = '$'.repeat(result.price);
newResult.querySelector('.result-thumbnail').src = result.thumbnail;
newResult.querySelector('.result-website').href = result.url;
resultArea.appendChild(newResult);
});
}
使用
你能在
fetch
方法中提供完整的url吗?我已经将hostname
定义为this.location.origin
你能在fetch
方法中提供完整的url吗?我已经将hostname
定义为this.location.origin