Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can'r Fetch data from database using Axios

I want to fetch data from database but can’t get it and when I console print it, is just empty.
Is there something wrong with my code?

         function har() {
            axios.get(url2, {
              headers: {
            'X-RapidAPI-Key': 'your-rapidapi-key',
            'X-RapidAPI-Host': 'body-mass-index-bmi-calculator.p.rapidapi.com',
              },

              })
              .then(function (response) {
               console.log(response);
   
              })
             .catch(function (error) {
               console.error(error);
              });
               }

This is my my php code

                    <?php
                    include 'db.php';



               
                    $emparray = array();

                    $sql = 'SELECT * FROM schedule_list';
                    $results = mysqli_query($conn,$sql);
                    while($row = $results->fetch_assoc()){

                        $emparray[] = $row;

                    }
                    $hey = json_encode($emparray)

                    ?>

I just want to get the data from the json encode to javascript without using react or vue, just plain vanilla javascript

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You aren’t outputting anything in your server code. Try instead to output the results like:

<?php
    include 'db.php';

    $emparray = array();
    $sql = 'SELECT * FROM schedule_list';
    $results = mysqli_query($conn,$sql);
    while($row = $results->fetch_assoc()){
        $emparray[] = $row;
    }
    echo json_encode($emparray);
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading