我很难从apptweak.com上获取并查询JSON文件。 执行代码时,出现catch错误“ [object error]” {}。 我需要帮助来解决这个问题。该请求仅接受HTTPS调用。我在第一次使用时遇到了cors问题,但是我用chrome扩展程序解决了这个问题。
<h1>Testing</h1>
<input type="text" id="" value="type something">
<button id="submit" onclick="getUsers()">Submit</button>
<div class="output-styling" id="output">
</div>
var url = "https://api.apptweak.com";
var OS = "/ios/";
var json = "searches.json?term=";
var country = "&country=";
var nameofcountry = "be";
var device = "&device=";
var typeOfDevice ="ipad";
// var name = "bingo";
var name = document.getElementById("name").value;
var headers = {
'method': 'GET',
'X-Apptweak-Key': 'Api key fo here',
'Cache-Control': 'no-cache',
'mode': 'cors'
};
function getUsers(){
fetch(url + OS + json + name + country + nameofcountry + device + typeOfDevice, {headers})
.then((res) => res.json())
.then((data) => {
let output = '<h2 class="mb-4">Free Evaluation</h2>';
data.forEach(function(searches){
output += `
<table class=" scroll ">
<thead>
<th width="200">ID</th>
<th width="200">Title</th>
<th width="200">Price</th>
<th width="200">Developer</th>
<th width="200">Device</th>
<th width="200">Rating</th>
<th width="200">Slug</th>
</thead>
<tbody>
<tr>
<td>${content.id}</td>
<td> ${content.title}</td>
<td>${content.price}</td>
<td>${content.developer}</td>
<td>${content.devices}</td>
<td>${content.rating}</td>
<td>${content.slug}</td>
</tr>
</tbody>
</table>
`;
});
var display = document.getElementById('output');
display.innerHTML = output;
})
.catch(function(error) {
console.log('Looks like there was a problem: \n', error);
});
}