Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:51:50+00:00 2026-05-27T13:51:50+00:00

Hello fellow programmers! I just started an additional programming project and swore to god

  • 0

Hello fellow programmers! I just started an additional programming project and swore to god my code will bo SO much cleaner and easily upgradeable than it has been before.

Then I stumbled upon my “arch enemy” the jQuery AJAX returning. Last time I wanted to return something from an AJAX call I had to bend over and just make the call synchronous. That made things sticky and ugly and I hope that this time I will find something better.

So I have been googling/searching stackoverflow for a while now, and just don’t understand this solution many ppl has gotten which is called callback function. Could someone give me an example on how I could exploit these callback functions in order to return my login statuses:

function doLogin(username, password) {
    $.ajax({
        url:        'jose.php?do=login&user='+username+'&pass='+password,
        dataType:   'json',
        success:    function(data) {
                        if(data.success==1) {
                            return('1');
                        } else {
                            return('2');
                        }
                        $('#spinner').hide();
                    },
        statusCode: {
                        403:function() {
                            LogStatus('Slavefile error: Forbidden. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        },
                        404:function() {
                            LogStatus('Slavefile was not found. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        },
                        500:function() {
                            LogStatus('Slavefile error: Internal server error. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        },
                        501:function() {
                            LogStatus('Slavefile error: Not implemented. Aborting.');
                            $('#spinner').hide();
                            return (3);
                        }
                    },
        async:      true
    });
}

So as you probably know, you cannot use return the way I have done from inside an AJAX call. You should instead use callback functions which I have no idea of how to use.

I’d be VERY greatful if someone could write me this code using callback functions and explaining to me just HOW they WORK.

EDIT:

I REALLY need to return stuff, not use it right away. This function is being called from within another function and should be able to be called from different places without being rewritten even slightly.

/EDIT

Sincerly,

Akke

Web Developer at Oy Aimo Latvala Ab

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:51:50+00:00Added an answer on May 27, 2026 at 1:51 pm

    There are three parts to the basic “I need an asynchronous callback” pattern:

    1. Give the function a callback function parameter.
    2. Call the callback function instead of returning a value.
    3. Instead of calling the function and doing something with its return value, the return value will be passed to your callback function as a parameter.

    Suppose your synchronous mind wants to do this:

    function doLogin(username, password) {
        // ...
        return something;
    }
    
    switch(doLogin(u, p)) {
    case '1':
        //...
        break;
    case '2':
        //...
        break;
    //...
    }
    

    but doLogin has to make an asynchronous call to a remote server. You’d just need to rearrange things a little bit like this:

    function doLogin(username, password, callback) {
        return $.ajax({
            // ...
            success: function(data) {
                if(data.success == 1)
                    callback('1');
                else
                    callback('2');
            },
            //...
        });
    }
    
    var jqxhr = doLogin(u, p, function(statusCode) {
        switch(statusCode)) {
        case '1':
            //...
            break;
        case '2':
            //...
            break;
        //...
        }
    });
    

    The jqxhr allows you to reference the AJAX connection before it returns, you’d use it if you needed to cancel the call, attach extra handlers, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Again my fellow programmers out there, I'm designing and programming from scratch a
Hello fellow programmers! Recently, I started learning Git, and pretty quickly discovered the amazing
Hello Fellow Computer People! Anyone willing to help will have my gratitude ;) Just
Hello fellow programmers, I have made a function that looks up the database if
Hello fellow programmers, got a few problem here. I am adding a user control
Hello my fellow Stackoverflownians :), I just came across this thing called Zend. And
Hello fellow programmers. I have a SQL Server 2005 query that is taking a
Fellow JQuery hackers, hello :-) Suppose you have the following code: $('.links').click(function(){ //Do something
Hello fellow programmers I have been searching the internet for a few days now
my fellow developers! I hope very much that at least some of you will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.