javascript - Ajax call does not assign a value in the done promise -


this question has answer here:

i have method this:

var isnameunique = false;  function validatename() {          var url = "/somerules/checkifnameexists/";          var request = $.ajax({             url: url,             method: "get",             data: { sname: name},             datatype: "json"         });         request.done(function (result) {             if (result.doesnameexists) {                 alert("name exists!");                 console.log("name exists!");             }             else {                 isnameunique = true;                 console.log("name unique!");             }          });          request.fail(function (jqxhr, textstatus) {             console.log(textstatus);             alert("request failed.");         });      console.log("exiting validatename()"); } 

this called so:

function createnewuser() {     validatename();     console.log(isnameunique);     if(isnameunique){      // stuff     } } 

when run application have these in console in order:

  1. exiting validatename()
  2. false
  3. name unique!

when it's printing 3rd console expect 'isnameunique' set true. that's not happening!!! doing wrong?

thanks in advance.

by default every ajax request asynchronous, means code next ajax call not wait completion of ajax call. need is, make ajax call synchronous, next lines of code execute after completion of ajax call.

to make ajax call synchronous add following option in ajax setting:

async : false 

for more info check this documentation , read async setting.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -