2

I have below js function, I need to call /print2 function without clicking any buttons. I tried to write ajax to that. By the way, I am newer in ajax and js.

Where is the problem ı could not see ?

Thank you...

<script type="text/javascript" >
     function plot() {


    $.ajax({
        url: '/print2',
        success: function(data) {
            console.log('get info');

            $('#description').html(data['description']);
        }
    });


}

   plot()
</script>

It says

(index):30 Uncaught ReferenceError: $ is not defined at plot ((index):30) at (index):38

Where is the problem ?

Edit

$(document).ready(function () {
  //your code here
});

I wrapped my function with this code. As it stated here JQuery - $ is not defined

However, I it is not solve my problem.

Community
  • 1
  • 1
ozer
  • 241
  • 2
  • 12

1 Answers1

1

It looks like you do not have jQuery. To get jQuery you include the following script at the end of the body tag in your HTML.

<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>

make sure it is before this closing tag below.

</body>
thatguy
  • 1,249
  • 9
  • 26
  • 1
    @ozer jQuery is a JavaScript library - think of it as an "extension" to JavaScript. All references to `$` in your code are trying to reference jQuery functions, but without including jQuery, you can't use jQuery functions. – Tyler Roper Sep 18 '19 at 14:41
  • 1
    @ozer Ajax is built into jQuery. You can not use one without the other. This script tag tells the browser to load jQuery into your page. Without it, you will not be able to use any jQuery functionality. If the answer fixed our issue please select it as closed to let others know that the issue has been solved. – thatguy Sep 18 '19 at 14:44
  • @thatguy *"You can not use one without the other. "* - this is incorrect. You can certainly use jQuery without AJAX, and you can certainly use AJAX without jQuery. – Tyler Roper Sep 18 '19 at 14:49
  • @TylerRoper you can use 'XMLHttpRequest' without jQuery which is technically AJAX, but the '$.ajax' syntax is a part of the jQuery framework which you will need to use it. That is an argument of semantics. While technically the same, but still different. – thatguy Sep 18 '19 at 14:59
  • "You can't use one without the other" implies that you can't use jQuery without AJAX, which doesn't really make sense. It seems you're specifically referring to `$.ajax`, in which case that's correct, you cannot use this specific method without jQuery. I think the issue may lie in semantics, but the end result is a comment that is misleading/wrong. AJAX is completely supported without jQuery (`fetch`, `XMLHttpRequest`, etc) - however *the jQuery `$.ajax` method, specifically* is not. – Tyler Roper Sep 18 '19 at 15:00
  • 1
    @TylerRoper True, and I can see your issue with the comment and understand where you are coming from. My comment was more directed towards the $.ajax syntax the OP is using while also not trying to confuse or overwhelm a newer developer. Keeping it simple in the beginning is key to learning. Deep dives into the technologies and semantical differences will only confuse and deter someone who is just learning. – thatguy Sep 18 '19 at 15:07
  • @thatguy Agreed. However, and this is important, please note that everything on StackOverflow, from questions, to comments, to answers, are supposed to work towards building a repository of useful Q&A-style knowledge **for everyone**. Any time you answer or comment on a question, consider that the content is for an audience of thousands and thousands, as opposed to solely the one person who asked the question :) – Tyler Roper Sep 18 '19 at 15:09
  • 1
    @TylerRoper makes sense. I will be sure to be more explicit in the future. – thatguy Sep 18 '19 at 15:11