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
https://forums.phpfreaks.com/topic/259858-getjson-scope-problem/
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;
                        }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.