Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <title></title>
  6.   </head>
  7.   <body>
  8.     <form id='search'>
  9.       <input id='query' type='text'/>
  10.     </form>
  11.     <div id='search-results'></div>
  12.     <script src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
  13.     <script>
  14.     $("#search").submit(function(event) {
  15.       event.preventDefault();
  16.       var userInput = $("#query").val();
  17.       getRequest(userInput);
  18.     });
  19.  
  20.     function getRequest(userInput){
  21.       $.ajax({
  22.         url: "http://www.giantbomb.com/api/search",
  23.         type: "GET",
  24.         data: {
  25.           resources: "game",
  26.           query: userInput,
  27.           api_key: "YOUR_API_KEY_HERE",
  28.           format: "jsonp",
  29.           crossDomain: true,
  30.           limit: 15,
  31.           field_list: "platforms,name,image,original_release_date,site_detail_url",
  32.           json_callback: 'showResults'
  33.         },
  34.         dataType: "jsonp"
  35.       }).done(function(data) {
  36.         showResults(data.results);
  37.         console.log(data);
  38.       });
  39.     }
  40.  
  41.     function showResults(result) {
  42.  
  43.       var html = "";
  44.       $.each(result.results, function(index, value) {
  45.         var gameName = value.name;
  46.         var boxArt = value.image ? value.image.icon_url : '';
  47.         var releaseDate = value.original_release_date;
  48.         var platform = value.platforms.name;
  49.         var site_detail = value.site_detail_url;
  50.         html += "<li><p>" + gameName + "</p></li>" + "<img src=" +boxArt + ">" + "<p>" + releaseDate + "</p>" + platform + "</p>" + "<a href='" + site_detail + "'><p>Click here for more information</p></a>";
  51.       });
  52.  
  53.       $("#search-results").html(html);
  54.  
  55.     }
  56.     </script>
  57.   </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement