在邮递员中,当我在响应中完整使用文章时使用news属性下方的url
https://api.currentsapi.services/v1/search?country=us&language=en&apiKey="..."
但是在javascript中,我得到了一个空的新闻数组。
这是JavaScript:
function displayNewsResults(responseJson){
console.log(responseJson)
}
let newsBaseUrl = 'https://api.currentsapi.services/v1/search'
let newsApiKey = '...';
function getNews(country) {
let params = {
country: country,
language: 'us',
apiKey: newsApiKey
}
let queryString = formatQueryParams(params);
let url = newsBaseUrl + '?' + queryString;
console.log(url)
let req = new Request(url);
fetch(req)
.then(response => {
if (response.ok) {
return response.json();
}
else {
throw new Error(response.statusText);
}
})
.then(responseJson => displayNewsResults(responseJson))
.catch(err => {
$('#js-error-message-news').empty().text(`Something went wrong: ${err.message}`).show();
})
}
函数displayNewsResults
返回:
{status: "ok", news: Array(0), page: 1}
答案 0 :(得分:2)
代码似乎正确,结果也正确,但是错误是,在您的代码中,您使用language
参数设置为us
而不是en
来调用API