'Ajax call to iTunes Search API with nodejs javascript and express

multiple similar questions out there but I cannot figure out this iTunes api call.

here is part of my server.js

    app.post('/look', function(req,res){
    var encoded = encodeURIComponent(req.body.search);var itunesURL = 'https://itunes.apple.com/search?';
      var apiURL = itunesURL + 'term=' + encoded + '&country=US&media=music&limit=5';
      console.log("inside it!");
      var search_value = req.body.search.body;
      $.ajax({
        type: 'POST',
        url: 'https://itunes.apple.com/search?',
        data: { term: search_value },
        // kind:'song', trackName: "Upside Down",
        dataType:"json", //nodejs knows its trying to send me json
    
        data:JSON.stringify({  // puts into a json obj 
            "search": $("#search").val(),
        }),
        success:(function(result){ // result is json from node
          console.log("YESASASAS")
            $.each(result[5].trackName, function(key,value){
                $('#search_results').innerHTML += "<div> Track Name: </div><div> "+ result.trackName + "</div> <div> Artist Name: </div> <div> "+ result.artistName + "</div>"
            });
        }),
        error:function(e)
        {
            console.log("EROROROROR")
        },
      
      });
    });

And some of my ejs file where I just have an input box and submit button to call the ajax api call

    
    <form action="/look" id = "doit" method="post">
    <!-- search box -->
    <input type="text" placeholder="Search.." id="search"><br>
    <!-- search button -->
    <input type="button" action="/look" onclick="myFunction()" id="search_button" value="search">
    <!-- the div that will contain our search results -->
    <div id="search_results" style="padding:5px;"> Search Results: </div>
    </form>
    <p id="demo"></p>

Would love another method to do this other than the button calling the /look endpoint



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source