Jump to content

.getJSON scope problem


The Letter E

Recommended Posts

I wasn't sure if this was best to post in the ajax section, since the ajax function is working just fine, but here goes.

 

My Code:

function validate(form){
                                var errors = {};
                                        url = 'http://mybox.vm/login?username='+form.username;
                                        jQuery.getJSON(url, function(data){
                                                if(data.response == "1"){
                                                        //THE CODE DEFINITELY MAKES IT TO HERE, IVE TESTED IT
                                                        errors.username = "That username is taken";
                                                }
                                        });
                                //"errors.username" never made it into the object
                                return errors;
                        }

 

I think i'm just missing something really simple but still have not found what.

 

Any help is appreciated.

 

 

Thank You,

 

E

Link to comment
Share on other sites

I wasn't sure if this was best to post in the ajax section, since the ajax function is working just fine, but here goes.

 

My Code:

function validate(form){
                                var errors = {};
                                        url = 'http://mybox.vm/login?username='+form.username;
                                        jQuery.getJSON(url, function(data){
                                                if(data.response == "1"){
                                                        //THE CODE DEFINITELY MAKES IT TO HERE, IVE TESTED IT
                                                        errors.username = "That username is taken";
                                                }
                                        });
                                //"errors.username" never made it into the object
                                return errors;
                        }

 

I think i'm just missing something really simple but still have not found what.

 

Any help is appreciated.

 

 

Thank You,

 

E

 

That took me way too long to figure out.

 

Solution:

//make the errors var global...duh
var errors = {};

function validate(form){
                                        url = 'http://mybox.vm/login?username='+form.username;
                                        jQuery.getJSON(url, function(data){
                                                if(data.response == "1"){
                                                        //THE CODE DEFINITELY MAKES IT TO HERE, IVE TESTED IT
                                                        errors.username = "That username is taken";
                                                }
                                        });
                                //"errors.username" never made it into the object
                                return errors;
                        }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.