0

I'm fairly new to JS. Right now, I have a JSON file on my Express server. Here is how I did that:

const data = require('./src/data.json')


app.get('/search', function (req, res) {
  res.header("Content-Type",'application/json');
  res.json(data);
});

Now, I need to access the dictionaries in that JSON file to use the text throughout my web app. I know I need to use Ajax and jQuery, but I'd love some explanation of how I could do that efficiently and easily. Here's a snippet of my JSON:

[{
    "name": "Il Brigante",
    "rating": "5.0",
    "match": "87",
    "cuisine": "Italian",
    "imageUrl": "/image-0.png"
}, {
    "name": "Giardino Doro Ristorante",
    "rating": "5.0",
    "match": "87",
    "cuisine": "Italian",
    "imageUrl": "/image-1.png"
}]

Any help would be immensely appreciated.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Theo Strauss
  • 1,281
  • 3
  • 19
  • 32
  • Are you asking how to use ajax with jQuery? Because there are thousands of tutorials on the internet telling you how to do that... – Heretic Monkey Jun 30 '17 at 17:59
  • Possible duplicate of [How to pass data from node js to html.?](https://stackoverflow.com/questions/42579057/how-to-pass-data-from-node-js-to-html) – Heretic Monkey Jun 30 '17 at 18:02
  • @MikeMcCaughan bro so unnecessary. first, you edit ONE LETTER. do you know who puts in that much effortness? losers. obviously i read the tutorials, but i came here, where nice people help. _cheers, theo_ – Theo Strauss Jun 30 '17 at 18:09
  • You make no mention of that research in your question. How would I know that you did that? Hence my question. The edit revision history shows I did more than edit one letter, and linked to the Meta post explaining what I did remove. Those tutorials would have made mention of the functions in the answer you have received, so there is further evidence of a lack of research. – Heretic Monkey Jun 30 '17 at 18:13
  • @MikeMcCaughan Sorry, you changed one letter AND took away "cheers, theo". what a revision. thank you so much. really helped out. – Theo Strauss Jun 30 '17 at 18:21

1 Answers1

2

You can use $.ajax function:

$.ajax({
   dataType: "json",
   url: 'url here',
   data: data,
   success(response) {
      console.log(response);
   }
});

Or use shorthand for that:

$.getJSON('url here', function(response) {
   console.log(response);
});

Either one is fine

Azat Akmyradov
  • 176
  • 2
  • 2
  • 10