API fail to load for XMLHttpRequest using Json file

I’m trying to create a search engine using XMLHttpRequest to pull data from a JSON file. Then show that data in a new tab. When I test my site I don’t receive any errors.


<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
<script src = "script.js"></script>


    // xmlRequest
    xhrRequest.addEventListener('DOMContentLoaded', function (event){    
// handling enter key event



            alert('No hero found! Try selecting the hero from the list');

Thank you

PD: there is a problem in the “architecture”

This code will run a full XHR request everytime a person times a key.

What you need is to call to the database when page is loading, and store all the superheros in localStorage.

When someone types you just search through that object, and could provide the user a ‘search by’ option also, because you don’t need a call to db on every action.

So I would do get data just call to db on page load, in pseudocode:

document.onload= function(e){
//the XHR request
// store in  localStorage.

then run events.

Thank you for your feedback! For the pseudocode you provided :

document.onload= function(e){
//the XHR request
// store in  localStorage.

I couldn’t find a lot of info on document.onload should I reference window.onload instead?

Thanks again!

you are welcome @pcoding , i don’t have enough time now but if your code is on github send it to me and i will take a look, test locally, and suggest changes.

as for the onload i mispelled it, but it needs to be an event listener that fires up very soon, so you get the data before the user actually clicks on anything.

@ santimir Yes, my link is GitHub - patrice111/SuperSearch: Superhero Search Engine
Thanks again!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.