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:

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

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

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

    // Perform the request:

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?




    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!


Share this post

Link to post
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

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...