-1

I am trying to use JSON.parse() in java script but it says json is not defined.
Why does my browser transform JSON into json ? How can I fix it ?
Note : I use <script type="text/javascript" src="jquery/jquery-3.6.0.min.js"></script> just before.
Here's my code :

$.getJSON("data.json", function(json) {
    console.log(json);
     });
      const queryString = window.location.search;
      const urlParams = new URLSearchParams(queryString);
      if (urlParams.has("target")) {
        console.log("Redirecting ...");
        var obj = JSON.parse(json);  // says json is not defined
        eval("window.location.replace(obj." + urlParams.get("target") + ");")
      } else {
        console.log("Missing target argument, printing error ...");
        document.getElementById("demo").innerHTML = "Missing target argument !";
      }

And the error : "Uncaught ReferenceError: json is not defined at line 7".
Thank you !

SiniKraft
  • 9
  • 3

1 Answers1

0

Thanks to Amy and Barmar, the problem was just that json was from outer scope, so I put all my code in the json function and it works great ! Thanks

SiniKraft
  • 9
  • 3