I want to click a button to display API data using JavaScript Fetch but I'm getting trouble with TypeError: NetworkError when attempting to fetch resource.
I think it has to do with the HTML form. If someone can help me with this. I'm trying to fetch data from NASA APIs.
The JS Code is below
var search = document.getElementById("search");
search.addEventListener("click",function(){
//var startdate = document.getElementById("starter");
//var enddate = document.getElementById("enddate");
var url = "https://api.nasa.gov/planetary/apod?api_key=key";
fetch(url,{
method:"GET",
headers:{
"Content-Type":"application/json"
},
mode:"cors",
catch:"default"
}).then(function(response){
if(response.ok){
return response.json();
}else{
throw new Error(Error);
}
}).then(function(data){
console.log(data);
}).catch(function(error){
console.log(error);
});
});
Below is the HTML form
<form>
<input type="date" id="startdate" placeholder="enter a start date"><br>
<input type="date" id="enddate" placeholder="enter a start date"><br>
<button type="submit" id="search">Search</button>
</form>
Thanks