Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hey! New to these forums, but I just started web dev and about to finish the book. I am having a hard time with one thing. Well, the entire Ajax chapter is hard for me, but one part in particular I don't understand, it is a chunk of code inside the login.js for the section.

 

// Create an object of Ajax options:
    var options = new Object();

    // Establish each setting:
    options.data = data;
    options.dataType = 'text';
    options.type = 'get';

    options.success = function(response) 
    {
            // Worked:
            if (response == 'CORRECT') {

                    // Hide the form:
                    $('#login').hide();

                    // Show a message:
                    $('#results').removeClass('error');
                    $('#results').text('You are now logged in!');

            } else if (response == 'INCORRECT') {
                    $('#results').text('The submitted credentials do not match those on file!');
                    $('#results').addClass('error');
            } else if (response == 'INCOMPLETE') {
                    $('#results').text('Please provide an email address and a password!');
                    $('#results').addClass('error');
            } else if (response == 'INVALID_EMAIL') {					
                    $('#results').text('Please provide your email address!');
                    $('#results').addClass('error');
            }

    }; // End of success.
    options.url = 'login_ajax.php';

    // Perform the request:
    $.ajax(options);

1) is this line options.success = function(response) suppose to return a boolean value?

 

2) also on the same line, options.success = function(response), i am having huge problem understanding where response came from. This parameter variable is not declared anywhere or passed in, so where did it come from?

 

3) 

 

    options.data = data;
    options.dataType = 'text';
    options.type = 'get';
     ......
     options.url = 'login_ajax.php';
 
are these names, (data, dataType, type, and url) always required for ajax? or can this (or ajax in general) be done in different ways?
 
4)  $.ajax(options);
 
can you explain this more? 
 
 
 
thanks in advance guys!

 

Link to comment
Share on other sites

Hi and welcome to the forum.

 

I don't have this particular book but you should be able to see what the AJAX call can return from the code in login_ajax.php.

 

From your code the response from the AJAX call can be either 'CORRECT', 'INCORRECT', 'INCOMPLETE' or 'INVALID EMAIL', so not a Boolean.

 

You can make an AJAX call in several different ways; this one is via 'GET' and an object named options supplies the information to be sent to the login_ajax.php script.

 

There are lots of guidance on AJAX via Google as well as the valuable information in Larry's books. Bottom line an AJAX call invokes a script (login_ajax.php in this instance) without leaving the web page you are on. The callback function then processes the returned data from called module (login_ajax.php in this instance) but it processes it in the web page you are on. You can see DOM elements being updated by the callback function, e.g., where 'You are now logged in!' is being inserted into the DOM.

 

The code you have posted is using jQuery and there are lots of information on that too available. jQuery, used correctly, is a fantastic tool.

 

As I don't have this particular book, I'm sorry that I can't help more.

 

Best wishes.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...