0

In my current project, I want to use Chart.js to display certain data from the database. Now I have the problem that I am not given any data, instead I always get the error message "$ is not a function" in Chrome. I already tried some things, unfortunately without success. I also included the latest version of JQuery at the top of my JS scripts.

The values ​​of my MySQL database I get through a PHP script, which this out in JSON. There I get all the data.

My JS script calls:

<script src="vendors/jquery/dist/jquery.min.js"></script>
<script src="vendors/popper.js/dist/umd/popper.min.js"></script>
<script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="assets/js/main.js"></script>
<script src="assets/js/admin.js"></script>

<script src="vendors/chart.js/dist/Chart.bundle.min.js"></script>
<script src="assets/js/statistics/show-all-views.js"></script>

My Chart.js Code:

$(document).ready(function() {
    $.ajax({
        url: "http://localhost/fitnesslaeufer_cms/admin/includes/statistics/charts-data/show-all-views.php",
        method: "GET",
        success: function(data) {
            console.log(data);
            var views = [];

            for(var i in data) {
                views.push(data[i].score);
            }

            var ctx = document.getElementById("monthlyViews");
            ctx.height = 50;
            var myChart = new Chart(ctx, {

                type: 'line',
                data: {
                       labels: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
                       datasets: [ 
                           {
                              label: "My First dataset",
                              borderColor: "rgba(0,0,0,.09)",
                              borderWidth: "1",
                              backgroundColor: "rgba(0,0,0,.07)",
                              data: views
                           }
                      ] ........
1
  • 1
    Have you tried replacing $(document.ready with jQuery(document).ready(function(){.. ? It might also be worth checking your network tab to make sure jQuery is being loaded in the page and not resulting in a 404. Commented Sep 27, 2019 at 12:40

1 Answer 1

1

Use jQuery for your document and make sur to pass $ in the callback.

  jQuery(document).ready(function($) {
      // Your code here
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.