0

I'm having a small problem to do with capturing variable values in anonymous functions.

Here's my code.

function register(email, username, password) {

    var postFields = {
        email: email,
        username: username,
        password: password
    };

    var response = null;

    $.post("../ajax/register/", postFields, function(result) {
        response = result;

        //I can print the contents of the variable response here
        console.log("First " + response);
    });

    //But it prints null when I try to print it here
    console.log("Second " + response);

    return JSON.parse(response);
}

The post request returns a perfectly fine JSON response so I'm wondering what the issue could be.

Any help on this would be appreciated.

Toph
  • 1
  • 4

0 Answers0