Quiz

  1. In the Ajax graphic, the back-end code is written in Python. Are back-ends usually written in Python? Wouldn't that be slow?

    Backends are written in Python, JavaScript, PHP, Ruby, Perl, and many other languages. Not usually very fast ones.

    Python is slower than C for raw computation, but it's not that slow. Web applications are typically I/O bounds (limited by speed of I/O rather than speed of computation), so the speed of the programming language is usually unimportant.

  2. Just to clarify, jn the Adjustments to Truck part of the reading, the reason why the argument to this.db.get is because we're dealing with Ajax now and there needs to be a callback function?

    Yes, that's exactly right. Well done!

  3. What does it mean for a method to be a wrapper?

    Great question. Let's start with functions. Suppose I have a function that computes square roots, but it breaks if the argument is negative. I could do this:

    
        function sqrt(x) {
            if( x < 0 ) {
                throw new Error('negative argument');
            } else {
                return realSqrt(x);
            }
        }
    
    

    We would call sqrt a wrapper around realSqrt.

    Methods work the same way:

    
    class Account {
        constructor(init) {    
            this.acct = new RealAcct(init);
        }
        deposit(amt) {
            return this.acct.deposit(amt);
        }
        withdraw(amt) {
            return this.acct.withdraw(amt);
        }
    }
    
    
  4. Can you further explain why $.get() method is called a wrapper for $.post()?

    I think you mis-read that. Both $.get and $.post are wrappers around $.ajax

    
    class jQuery {
        constructor(init) {    
           ...
        }
        get(url, cb) {
            this.ajax({method: 'GET'} ... , cb);
        }
        post(url, data, cb) {
            this.ajax({method: 'POST'} ..., cb );
        }
        ajax(url, description, cb) {
            lowLevelAjaxCode(url, description, cb);
        }
    }
    
    
  5. could you explain .get(), .post(), and .ajax()? and when you would use each one

    Sure. This is based on MDN HTTP Methods and (simpler and easier) W3Schools HTTP Methods

    • use GET when retrieving information from the server (e.g. browsing Instagram)
    • use POST when updating information on the server (e.g. POSTing to Instagram)
    • use AJAX when neither of the above, such as DELETE.
  6. how are errors handled when using callback functions in ajax?

    Great question! Let's briefly look at the jQuery documentation

  7. How can we test if our ajax code is working? I tried the codes in my console but i kept getting some values undefined even though i inserted some url and some objects.

    Wonderful question! Thanks so much for trying these. I'll demo some stuff in CoffeeRun:

    
        ds1 = debug.ds1;
        setg = function (data) { g = data; };
        ds1.getAll(setg);
        ds1.get('fred@hogwarts.ac.uk', setg);
        $.get('https://oldcs.wellesley.edu/cs204cloud/coffeerun/fred@hogwarts.ac.uk');
        $.get('https://oldcs.wellesley.edu/cs204cloud/coffeerun/fred@hogwarts.ac.uk', setg);
    
    

    And, in the CS 304 Ajax movie rating app:

    
        getRating('666');
        rateMovie('666', 3);
        getRating('666');
        putRating('666', 4);
        getRating('666');
        deleteRating('666);    
    
    
  8. There is nothing more I would like to talk about. / Everything is clear

    Great!