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?
2
Answers
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.
That console.log written like this will return "data" as a text and not as a variable, try with:
Instead of