1

My code could display the data without the onclick function but when it's added the data does not display in my table. There are no errors being displayed as well.

This is the code to get the json data and display:

<script>  
            var url = "http://127.0.0.1:8080/Assignment2/video/backend/RestController.php?view=all";
            
            $('#viewUser').on('click',function (e){
                    $.ajax({
                    
                    method: "GET",
                    cache: false,
                    url: url,
                    success: function(data) {
                        var student = '';

                    // ITERATING THROUGH OBJECTS
                    $.each(data, function (key, value) {

                        //CONSTRUCTION OF ROWS HAVING
                        // DATA FROM JSON OBJECT
                        student += '<tr>';
                        student += '<td>' + 
                            value.id + '</td>';
                           
                        student += '<td>' + 
                            value.username + '</td>';

                        student += '<td>' + 
                            value.videoName + '</td>';

                        student += '</tr>';
                    });
                    
                    //INSERTING ROWS INTO TABLE 
                    $('#table').append(student);
                    },
                    error:function(exception){alert('Exeption:'+exception);}
                    })
                    e.preventDefault();
                });
        </script> 

This is the code for the button (for onclick function):

<h2>View Stored User</h2>
  <button type="button" id="viewUser">GO</button> 

[The error i'm receiving from the console log of the browser tho it's not related][1] [1]: https://i.stack.imgur.com/7TZ3a.png

Thatt
  • 61
  • 7
  • Please change the code in your question to a [mcve]. Also, did you check for errors in your browser's console? If there are any, please add those to the question as well. – Ivar Jun 18 '21 at 09:51
  • There's no element with the id viewUser in the code you shared with us. – Quentin Jun 18 '21 at 09:54
  • @Quentin i have added the code for it – Thatt Jun 18 '21 at 10:03
  • My best guess would be [this issue](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element). But without more information, it is impossible to know for sure. – Ivar Jun 18 '21 at 10:03
  • @Ivar i have added the error from the console log – Thatt Jun 18 '21 at 10:04
  • 1
    @Ivar — That's my guess too, but even with the edit there's no clue about how the two bits of code relate to each other. – Quentin Jun 18 '21 at 10:06
  • 1
    @Ivar solved it, moved the script further down to page and it worked. Thank you! – Thatt Jun 18 '21 at 10:11

0 Answers0