I'm using NewYorkTimes API that gets the search query from the input field, however, when I press enter after typing the input, my localhost reloads and navigate to localhost:3000/?
I console.log(url)
in the console and the url is correct as the JSON request returns results, but the results aren't displayed in HTML since the page navigates automatically. Not sure what may be causing this to happen.
form
span.material-icons search
input.form-control(id="input", placeholder="Search for articles, headlines, etc.")
// SHOWS SEARCH RESULTS WHEN THE USER PRESSES ENTER
$("#input").keypress(function(event) {
if (event.keyCode == 13) {
searchArticles(this.value);
$("#input").val("");
}
});
// MAKES THE REQUEST AND DISPLAYS THE NEWS BASED ON RESULTS
function searchArticles(term) {
term = term.replace(" ", "+");
searchURL = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=" + term + "&api-key=" + searchAPI;
console.log(searchURL);
$.getJSON(searchURL, function(api) {
api.response.docs.forEach(function(data) {
link = data.url;
cardTitle = data.headline;
postedBy = data.byline.original;
createCardElements();
});
});
}
This is the sample JSON request: