0

It is a simple autocomplete program. The value entered in the textbox is read. We can see it while debugging but the GetJson() property fails to fetch the JSON file. Thus the program fails to execute.

My code is as follows

JavaScript code The getJson prop fails to fetch the file. Screenshot attached to the question The HTML, CSS, and JavaScript are in a single file. Its named Search_Web.html

<script>
 

 $(document).ready(function() {
    $("#search").keyup(function() {
      $("#result").html("");

      var searchField = $("#search").val();
      var expression = new RegExp(searchField, "i");
      $.getJSON("data.json", function(data) {
        $.each(data, function(key, value) {
          if (value.name.search(expression) != -1 || value.location.search(expression) != -1) {
            $("#result").append(
              '<li class="list-group-item><img src = "" ' +
                value.image +
                ' height="40" width="40" class="img-thumbnail" /> ' +
                value.name +
                ' |<span class= "text-muted"> ' +
                value.location +
                '</span></li>"'
            );
          }
        });
      });
    });
  });
</script>
<style>
  #result {
    position: absolute;
    width: 100%;
    max-width: 870px;
    cursor: pointer;
    overflow-y: auto;
    max-height: 400px;
    box-sizing: border-box;
    z-index: 1001;
  }

  .link-class:hover {
    background-color: #f1f1f1;
  }
</style>
<!DOCTYPE html>
<html>
  <head>
    <title>Search Operation</title>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="#" />

    <script
      src="https://code.jquery.com/jquery-3.3.1.js"
      integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
      crossorigin="anonymous"
    ></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script
      src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"
      integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30="
      crossorigin="anonymous"
    ></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" />
  </head>
  <body>
    <br />
    <br />

    <div class="container" style="width:900px;">
      <h2 align="center">JSON Live Data Search using AJAX and Jquery</h2>
      <h3 align="center">Player Data</h3>
      <br />
      <br />
      <div>
        <input type="text" name="search" id="search" placeholder="Search Player Details" class="form-control" />
      </div>
      <ul class="list-group" id="result"></ul>
    </div>
  </body>
</html>

JSON file data.json

Browser Console I have attached a screenshot of where I think the errors occur. I have seared all the answers regarding this question in a stack overflow. None have helped me. I hope I will be able to solve this. I am still learning. Please forgive ant silly mistakes. Any help is deeply appreciated.

[
  {
    "name": "Joe Augus",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\joe.jpg",
    "location": "Kochi,India"
  },
  {
    "name": "Ronaldo",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\ronaldo.jpg",
    "location": "Turin,Spain"
  },
  {
    "name": "Messi",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\messi.jpg",
    "location": "Barcelona,Spain"
  },
  {
    "name": "Pogba",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\pogba.jpg",
    "location": "Manchester,UK"
  },
  {
    "name": "Rashford",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\rashford.jpg",
    "location": "Manchester,UK"
  },
  {
    "name": "Kroos",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\kroos.jpg",
    "location": "Madrid,Spain"
  },
  {
    "name": "Modric",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\modric.jpg",
    "location": "Madrid,Spain"
  },
  {
    "name": "Mbappe",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\mbappe.jpg",
    "location": "Paris,France"
  },
  {
    "name": "Neymar",
    "image": "C:\Users\BizFirst\Documents\Visual Studio 2015\Projects\First_Search\First_Search\Pics\neymar.jpg",
    "location": "Paris,France"
  }
]
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • I think your solution is just fine I tried putting it on the codesandbox example here: https://codesandbox.io/s/p9oy5v367m and it looks like it is working fine in terms of AJAX. though index.html and data.json need to be kept together on the server. – Pankaj Jan 04 '19 at 10:07
  • "Browser Console I have attached a screenshot of where I think the errors occur." — There's no error message in that screenshot. – Quentin Jan 04 '19 at 12:14
  • That should work fine, as long as you can access the file from the Url `http://localhost:60001/data.json`. What happens if you open that Url in your browser? – Reinstate Monica Cellio Jan 04 '19 at 12:16
  • What makes you say that the get request fails? – Reinstate Monica Cellio Jan 04 '19 at 12:37

1 Answers1

1

Wait, are you trying to access the file on the client machine (obviously since you got no URL)?

That will not work however you try it for safety reasons! Imagine If I could make a website that searches your disk for files and uploads them to me. Wouldnt that be terrible? Are you sure you need the file from client's machine? Are you simply trying to get JSON from the server and you forgot to put the URL? You must know that the JS is executed on the client's machine (browser to be specific), not on the server. How will you ensure that each of the people browsing your page has the said file?

Here are the few suggestions:

  1. You can hard-code data from JSON into JS object then load it as a script.

  2. If you need to emulate a server on your machine for testing purposes, you can set up one quick with Node and npm: local-web-server Or simply google how to setup localhost server and pick your poison :)

  3. The closest you can get to it reading an actual file from client's machine. I am aware that HTML5 fileReader facility does allow you to process local files, but these MUST be selected by the end user.

Anki
  • 407
  • 3
  • 8
DanteTheSmith
  • 2,937
  • 1
  • 16
  • 31
  • While this may be relevant, I see nothing in the question that suggests they're trying to open a file on the client. In fact, since they describe the contents of the file as autocomplete data then I seriously doubt they expect that file to reside on the user's machine. – Reinstate Monica Cellio Jan 04 '19 at 12:18
  • The script tag is an HTML tag? And inside it, there is a $.getJSON("data.json" ... There should be URL if the file is remote or am I missing something? – DanteTheSmith Jan 04 '19 at 12:29
  • I think you are. If it's just a filename then it's relative to the location of the html file that the script is being run in. In the example that the OP has posted the file would need to be located at `http://localhost:60001/data.json`, and then it *should* all work. Also, if they were trying to open a client file it would definitely need a full Url, but that Url would have to begin with `file://` (which wouldn't work, as you say). – Reinstate Monica Cellio Jan 04 '19 at 12:32
  • Option 1 is not an answer. Option 3 has nothing to do with this question. Option 2 is not relevant. They're running this on a local server already. See the linked screenshot, titled *Browser Console*, and pay attention to the Url. That is also why I'm 99.99% sure that they are not trying to read a file from the client. I've already explained to you that the filename `data.json` is relative to where it is being called from, and I've given you the Url that it would expect to find that file on. Sorry, but this answer is simply incorrect. – Reinstate Monica Cellio Jan 07 '19 at 08:05