Return true in jquery ajax -
i using ajax within jquery check whether value exist in database or not. can utilize normal select query(php) this, have create alert popup if value exist. that's why decide way. done, problem due homecoming false in jquery not allow next line eventhough name not exist in database. how can give homecoming true in particular place?
$.ajax({ type:"post", url:"actionn.php", data:"name="+name, success:function(data){ alert(data); //if info not exist means should out of ajax function } }); homecoming false; //if info exists if(name=="") //data not exist means should come here { alert("please come in name"); homecoming false; }
as t.niese pointed out return true
(or false) within success
not doing expect do. (see first comment under question).
probably straight-forward way accomplish goal utilize whatever code have if user (doest not) exists within success
.
so move logic/code when user exists/doesn't exist within success
callback. for more insights should read of links provided in comments
edit: think miss understand concept of asynchronous request - given comments in code
$.ajax({ type:"post", url:"actionn.php", data:"name="+name, success:function(data){ alert(data); //if info not exist means should out of ajax function //ajax function has "exited" here success callback //this code executed when request action.php succesfull //as in response code 200/ok } }); //return false; //if info exists //nope, don't know if info exists code //executed _while_ (!) request action.php made! //there no guarantee when request returns , can't write code //here relies on response //if(name=="") //data not exist means should come here //{ // alert("please come in name"); // homecoming false; //}
what want write this:
$.ajax({ type:"post", url:"actionn.php", data:"name="+name, success:function(data){ if(name==""){ alert("please come in name"); }else{ alert("your name is: "+data); } } });
crisp , short. logic depending on server's response handleded inside success
callack. there no code after $.ajax
in way dependant on data
.
please refrain return
ing in $.ajax.success
- it's not believe is
for farther question i'd invite (again) read ajax call's: http://stackoverflow.com/a/14220323/1063730 think paragraph restructuring code highly interesting you.
jquery ajax
No comments:
Post a Comment