-2

I am trying to get data of "value" from a data file

{"success":"true","value":"500","items":"12","currency":"NOK"}

this is what i think should work.

<div class="value"></div>
    <script>
    $.get('URLTOFILE', function(data) {


    var text = `Value: ${data.value|| "Not Found"}`       

        $(".value").html(text);

    });

    </script> 
Nafeez Quraishi
  • 5,380
  • 2
  • 27
  • 34
Vue
  • 1
  • You can add the tag [tag:jquery] to your question. Also what is your problem exactly? Do you face an error? Did you try you snippet? Did you read the official [documentation](https://api.jquery.com/jQuery.get/)? – aloisdg Sep 19 '19 at 14:31
  • Possible duplicate of [How to get JSON from URL in JavaScript?](https://stackoverflow.com/questions/12460378/how-to-get-json-from-url-in-javascript) – aloisdg Sep 19 '19 at 14:33
  • What exactly is the question here? What are you having trouble with? – DavidDomain Sep 19 '19 at 14:33
  • Can you post full, runnable code? – Nico Haase Sep 19 '19 at 15:19

1 Answers1

0

Is your json file hosted on a server somewhere? If so, are you getting any errors when you attempt to fetch it?

If the json file is simply a file on your computer, you won't be able to load it like that. If you are using webpack or another package manager to bundle your files, you can use import to import the file into your javascript file like so:

import json from "file.json";

console.log("My json: ", json);

// Rest of javascript file here

This assumes that you've set up your bundler to handle json files though. Webpack does this for you automatically. So if you're using it, then this should work. Not sure about other bundlers though.

But it's best to host the file on a server and fetch it, especially if the data changes constantly - such as from an API.

always_testing
  • 181
  • 1
  • 1
  • 5