0

I'm attempting to grab Twitter posts using the API and have found the following code in a tutorial:

tweets.js

$(function(){
    $.ajax({
        url: 'get_tweets.php',
        type: 'GET',
        success: function(response) {
            if (typeof response.errors === 'undefined' || response.errors.length < 1) {
                var $tweets = $('<ul></ul>');
                $.each(response, function(i, obj) {
                    $tweets.append('<li>' + obj.text + '</li>');
                });
                $('.tweets-container').html($tweets);
            } else {
                $('.tweets-container p:first').text('Response error');
            }
        },
        error: function(errors) {
            $('.tweets-container p:first').text('Request error');
        }
    });
});

I'm calling it using this HTML, which I've modified slightly from the original, so it's now calling the latest, validated version of jQuery from the official site:

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Twitter v1.1 API - A JavaScript and PHP Solution</title>
    </head>
    <body>
        <div class="tweets-container">
            <p>Loading...</p>
        </div>
        <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"
        integrity="sha256-/SIrNqv8h6QGKDuNoLGA4iret+kyesCkHGzVUUV0shc="
        crossorigin="anonymous"></script>
        <script src="js/tweets.js"></script>
    </body>
</html>

However, when I load this into the browser I get the following error:

TypeError: $.ajax is not a function

Things I've tried:

  1. Checked if the ajax call changed since the code was originally written: looking through the API docs and it looks like the syntax is still valid.
  2. Took away the function wrapper.
  3. Wrapped the whole thing in

    $(document).ready(function() {...});
    
  4. Moved the scripts tag further up the body and even into the <head> section in case jQuery wasn't being properly loaded.

but nothing seems to work and I get the exact same error every time.

  • 2
    You're loading the "slim" version of jQuery, which "excludes ajax, effects, and currently deprecated code" (see https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/ under "Slim Build") – Paul Roub Jan 11 '17 at 19:36
  • Ah, brilliant. In the end it was a simple solution! –  Jan 11 '17 at 19:37

0 Answers0