0

I am trying to load some JSON data from an URL to populate a dropdown in a form.

I am using the getJSON() function to get the data, and it has worked for other URLs very well, but for this instance I don't know why it doesn't return anything.


let primaryValue = $("#cr5dd_workrequirement option:selected").val();
 
if (primaryValue != null && primaryValue != ""){

        $.getJSON("/workreq_results?id=" + primaryValue, function (data) {
            console.log("data");
        });

    }

The link is working fine and it is correct. I just want to see if the data is loaded correctly from the URL. This is the data found at the link:

{ "results": [ { "DeptID": "cad426c1-eec7-ed11-b597-000d3aa9a09b", "DeptName": "HR ", } ] }

I am getting no error from the console and the dev tools are showing that the products file is being loaded! I am seeing a status of 200 OK. But there is no data returned in the console. What issue may it be?

  • 1
    Change this line: console.log("data"); It must console.log(data) – 0x01010 Apr 03 '23 at 16:20
  • If the code is exactly as written in the question, you are logging the string `"data"`, not the variable `data`. @0x01010 has the right advice. – Heretic Monkey Apr 03 '23 at 16:29
  • *no data returned in the console* - not even the word "data"? Is your console filtered? Try adding `console.log("test")` just before your $.getJSON – freedomn-m Apr 03 '23 at 16:34
  • "data" was a typo, it should have been console.log(data). But even with that, it should have been the word "data" in the console, but nothing is returned, and yes the console is filtered, before that I showed a "test" message – Bogdan Rotaru Apr 03 '23 at 17:10
  • 1
    Such issues are best debugged in a browser development tools (assuming your JS runs in a browser). For example in Chrome press F12 to bring up the DevTools, go to Network tab, in the subwindow below identify your request line ("/workreq...") and click on it. Another sub-window should open to the right with various tabs showing the request and the response. Go to the "Response" tab to see what response was actually sent. If it's not the response you expect, issue likely on server side. Report what you see. – Martin Lisowski Apr 03 '23 at 19:31

2 Answers2

0

That console.log written like this will return "data" as a text and not as a variable, try with:

console.log(data);

Instead of

console.log("data");
0

I managed to see the issue, so going into the Response tab in Dev Tools in the browser, the data found seemed to be alright, but looking at it more closely, there was a , at the end of the last element of the JSON (after "HR"), so the content in the URL was not correct in terms of JSON syntax and thus the getJSON fails silently, without throwing any errors. I modified the JSON and now it's working fine, thanks.