2

I'm not sure why my code is bringing up an error. What have I got wrong?

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script>
      $( document ).ready(function() {
        var logResp = $('#food-hygiene');

        logResp.ajax({
                        url: "food-hygiene.html",
                        async: false,
                        type: "GET",
                        dataType: "string"
                    }).done( function(data) {
                        alert(data);
                    })
      });
    </script>
  </head>
  <body>
    <div class="container-fluid">
      <div class="content-wrapper">
        <div id="food-hygiene">Loading...</div>
         </div>

        </div>
        <!-- jQuery first, then Tether, then Bootstrap JS. -->
        <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
      </body>
    </html>

UPDATE: As pointed out by Satpal, there are two references to jQuery. The lower one is a slim version which doesn't support jQuery.

7
  • 1
    Instead of logResp.ajax use $.ajax Commented May 25, 2018 at 11:34
  • Instead of logResp.ajax({ do $.ajax({ Commented May 25, 2018 at 11:34
  • The "slim" version of jQuery doesn't include the $.ajax API. Use full version Commented May 25, 2018 at 11:38
  • 3
    code.jquery.com/jquery-3.1.1.slim.min.js which is added at bottom of page which is override previous loaded ibrary Commented May 25, 2018 at 11:40
  • 1
    Ah. Thank you for that. Commented May 25, 2018 at 11:42

4 Answers 4

1

logResp it contains the instance of the #food-hygiene element you select using this statement: $('#food-hygiene');.

Replace this code:

logResp.ajax({
                    url: "food-hygiene.html",
                    async: false,
                    type: "GET",
                    dataType: "string"
                }).done( function(data) {
                    alert(data);
                })

With this:

jQuery
    .ajax(
        {
            url: "food-hygiene.html",
            async: false,
            type: "GET",
            dataType: "string"
         }
    )
    .done(
        function(data) {
            alert(data);
        }
    );
Sign up to request clarification or add additional context in comments.

3 Comments

It solves one issue but doesn't solve the OP's problem.
You might want to check that the code is actually solving the OP's problem before saying that @BlindRoach.
We all come here to learn and share our knowledge, and I feel like the whole downvoting system was poorly made. I think you should have pointed that out as comment, the answer isn't something that was scribbled in a few seconds, its well enough constructed and I think he would edit/remove after a heads up. Didn't mean to offend by my comment, I just think the downvote should be reserved for ignorant out of context answers that don't even aim to fix the problem at hand.
1

I had the same issue.

Just replace:

'src="https://code.jquery.com/jquery-3.1.1.slim.min.js"'

with

'src="https://code.jquery.com/jquery-3.1.1.min.js"'

in script tag and add it in head tad

Comments

1

It seems the jQuery file is not loaded properly, I have tried without another <link> tag and the code doesn't throw any errors:

Also $('#food-hygiene') doesn't have a method ajax() put $ does.

$(document).ready(function() {
  
  $.ajax({
    url: "food-hygiene.html",
    async: false,
    type: "GET",
    dataType: "string"
  }).done(function(data) {
    alert(data);
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
  <!--<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>-->

</head>

<body>
  <div class="container-fluid">
    <div class="content-wrapper">
      <div id="food-hygiene">Loading...</div>
    </div>

  </div>
  <!-- jQuery first, then Tether, then Bootstrap JS. -->
  <!--<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</body>

</html>

Comments

0
  1. You are creating an object and trying to use it as a prefix for ajax which is wrong

  2. Add only one jQuery library either jquery-3.2.1.min.js or jquery-3.1.1.slim.min.js

  3. Put script code down on the page and check. (Or you can leave it there also)

Do like below:-

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  </head>
  <body>
    <div class="container-fluid">
      <div class="content-wrapper">
        <div id="food-hygiene">Loading...</div>
         </div>

        </div>
        <!-- jQuery first, then Tether, then Bootstrap JS. -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
         <script>
          $( document ).ready(function() {
            var logResp = $('#food-hygiene');

            logResp.ajax({
                            url: "food-hygiene.html",
                            async: false,
                            type: "GET",
                            dataType: "string"
                        }).done( function(data) {
                            alert(data);
                        })
          });
        </script>
      </body>
    </html>

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.