-1

Local HTML and JSON-Server data. Getting the data from JSON-Server and using it in JQuery.

Resolved with help of Kevin B and Sirko. Leaving the JS (JQuery) so it can be coppied in the future.

var i = 0;
var output;

$("#send").click(function(e) {
    $.getJSON("http://localhost:3000/db", function(data) {})
        .done(function(json) {
            $.each(json, function(key, val) {
                output+= '<p>name=' + json[i].name + '</p>';
                i++;
            });
            $("#paragraph").append(output);
        })
        .fail(function(jqxhr, textStatus, error) {
            var err = textStatus + ", " + error;
            console.log("Request Failed: " + err);
        });

});
  • 1
    Have you checked the console? I guess there's either a CORS or some parser error. – Sirko Feb 02 '22 at 20:29
  • Console shows nothing. No problem. I click the button and I get an alert typed before the .getJSON (added it now). But console shows no problems in it. @Sirko – Jaime Garcia Feb 02 '22 at 20:35
  • if i had to guess, i'd say `The button im pressing is a simple input with #send as the ID.` is literal, and you have `id="#send"` which is incorrect. If not, add an error handler. (add an error handler anyway) – Kevin B Feb 02 '22 at 20:43
  • no, the id is "send" and i get it by JQuery with the selector (# meaning id) @KevinB – Jaime Garcia Feb 02 '22 at 20:56
  • Welcome to Stack Overflow. Please provide a Minimal, Reproducible Example: https://stackoverflow.com/help/minimal-reproducible-example Also Check your Network Console and ensure you have a Request and Response Payload. – Twisty Feb 02 '22 at 20:56
  • I see you have a `
      ` element that you are appending Table Rows and Cells. This is not a good practice. You may need to consider how you are constructing the content.
    – Twisty Feb 02 '22 at 20:59
  • Changed it, think now is a bit better @Twisty now im appending to body in a new file. Trying to get the data – Jaime Garcia Feb 02 '22 at 21:02
  • What is happening inside the callback doesn't matter, if the callback isn't being called. We need information that would indicate why it isn't being called... such as network log data or console errors. – Kevin B Feb 02 '22 at 21:04
  • JSON-Server is not getting the call, URL is copied from the JSON-Server console and it doesnt show any GET or at least any type of information, telling me that it didn't ask JSON-SERVER for it. I cant comprehend why but url copied from json server is this one "http://localhost:3000/empleados" @KevinB – Jaime Garcia Feb 02 '22 at 21:08
  • 1
    that tells me your click event isn't being caught by this handler. which... you should be able to test. (and should have tested.) either getJSON is being called and theres an error, getJSON is being called and its successful, or it's not being called at all because your event handler isn't being called. – Kevin B Feb 02 '22 at 21:10
  • My click does work, as said, I used an alert below the function and above the .getJSON it gets inside the function and skips the .getJSON function @KevinB – Jaime Garcia Feb 02 '22 at 21:11
  • Exactly my point. What you're saying doesn't match up with what you are saying. You can't have a getJSON being called, and it not result in either success or error. it *will* either succeed or fail, there is no middle point – Kevin B Feb 02 '22 at 21:12
  • But the thing is I dont know why the getJSON function doesnt work. Looked it up and I dont think I have anything in a bad position or misspelled. Still trying and I'm not getting any response @KevinB – Jaime Garcia Feb 02 '22 at 21:14
  • If it's being called at all, you should see an entry in your network tab for it. You also should be able to get an error callback to occur – Kevin B Feb 02 '22 at 21:14
  • Okay thank you, checked the network tab and its there, the data appears there. But doesnt show on screen. I'm still very new here and this is my first post so sorry. @KevinB – Jaime Garcia Feb 02 '22 at 21:20
  • that's good, that means you can add a .fail( callback and see why jquery isn't calling the success callback. See the last example here: https://api.jquery.com/jquery.getjson/ – Kevin B Feb 02 '22 at 21:20
  • The page containing the above fragment, is it loaded from the very same URL, meaning something starting with `http://localhost:3000/` ? – Sirko Feb 02 '22 at 21:23
  • @Sirko Sorry for not saying it too, the page is local, and the json is in a json-server. Still, Very sorry. First question and I'm doing things worse – Jaime Garcia Feb 02 '22 at 21:24
  • Let me edit the above code and show how its now, now it doesnt show any error. Now I dont know how to append the data to a list. Do I do it after the fail? @KevinB – Jaime Garcia Feb 02 '22 at 21:36
  • well, if it fails, there's no valid data to append. the idea would be to log the failure to the console so you can investigate – Kevin B Feb 02 '22 at 21:37
  • @KevinB thank you so much for the help as well as Sirko . Got it to show in the web. Couldn't make it without your help. I'll edit the post and post the answer as well. Made a

    in the html just to see the data and it worked. Thank you so much again.

    – Jaime Garcia Feb 02 '22 at 21:45

1 Answers1

-1

Consider the following.

$(function() {
  $("#send").click(function(e) {
    console.log("Send clicked.");
    $.getJSON("./empleados", function(data) {
      console.log("Getting Items");
      var list = $("<ul/>", {
        class: "my-new-list"
      }).appendTo(".tablaemple");
      $.each(data, function(i, item) {
        var row = $("<tr>", {
          "data-nomina": item.nomina
        }).appendTo(list);
        $("<td>").html(item.nomina).appendTo(row);
        $("<td>").html(item.apellido + ", " + item.nombre).appendTo(row);
        $("<td>").html(item.base).appendTo(row);
        $("<td>").html(item.funcion).appendTo(row);
        $("<td>").html(item.flota).appendTo(row);
        $("<td>").html(item.tipoescalafon).appendTo(row);
      });
    });
  });
});

Your URL Path should be relative otherwise you might encounter a CORS issue.

This will make a Row and then add Cells for each item in data.

Please test this and post your results.

Twisty
  • 30,304
  • 2
  • 26
  • 45
  • Made some adjustments as Kevin B told me and now I retrieve data and. Tried the code but as my JSON isnt going to be local didnt work. I'll try downloading the json and putting it in the files. – Jaime Garcia Feb 02 '22 at 21:40
  • @JaimeGarcia if you're now getting Data, the rest of the jQuery Code I provided will work. You just need to update the URL to match what was working in your testing. – Twisty Feb 02 '22 at 21:53
  • Perfect, tried it and works, i'll mark yours as the answer. Do I keep the my post as is and not edit it more before closing the discussion? – Jaime Garcia Feb 02 '22 at 21:57
  • @JaimeGarcia if the question is answered, you can comment more. It is discouraged a bit. But if you have a new issue and need more help, post another question. – Twisty Feb 02 '22 at 22:01
  • So I can leave the question opened and keep on coding now because it works. Thank you so much again! – Jaime Garcia Feb 02 '22 at 22:02
  • @JaimeGarcia the question will always be available. Even Closed ones can be found. This helps others who are searching for similar issues to find the question and see the answer. – Twisty Feb 02 '22 at 22:03