jQuery ajax with multiple nested .when inside. Returns undefined -


jquery ajax multiple nested .when not returning properly. getting error "uncaught syntaxerror: unexpected token u" because of undefined variable.

below code , flow.

this method called on event of button click internally calls multiple methods dependencies. in below example flow masterprocess->buildandroidapk->unlockandroidkey

function masterprocess(thisform){   $.when(buildandroidapk()).then(function(result){     obj = json.parse(result);    }); }  function buildandroidapk(){   $.when(unlockandroidkey()).then(function(result){     obj = json.parse(result);      //there other .when based on obj response      return result;   }); }  function unlockandroidkey(){   //this function connects server via jquery ajax , gets json string inside success or error block   return '{"success":"1","message":"","content":null}'; } 

the function unlockandroidkey gets json string , able receive inside buildandroidapk. masterprocess receiving undefined string , json.parse results in error "unexpected token u".

i not sure whether have explained query if required can explain in more detailed.

your code not showing async operations, can't actual async code. that's need see in order you.

all sorts of issues:

  1. $.when() must passed 1 or more promises
  2. $.when() not needed @ if have 1 promise wait on can use .then() directly on single promise.
  3. buildandroidapk() , unlockandroidkey() must return promises
  4. the test json string trying return has syntax errors in (bad quoting)
  5. if you're using jquery , getting json server, jquery parse automatically - there's no need parse manually.

for code work way have structured, both buildandroidapk() , unlockandroidkey() must return promises. right now, aren't showing return of promise in either function. thus, when try use .then() on return value, won't work. or, when try pass $.when(), there's no promise wait on.

$.when() requires 1 or more promises passed it. buildandroidapk() method not return promise passing undefined $.when() has no promise wait on before calling .then() handler.

in addition, there no reason use $.when() unless have more 1 promise.


you aren't showing actual async portion of code it's little hard show how fix code, here's general idea:

function masterprocess(thisform){   buildandroidapk().then(function(result){     obj = json.parse(result);     // use obj here   }); }  function buildandroidapk(){   return unlockandroidkey().then(function(result){     obj = json.parse(result);      //there other .when based on obj response      return result;   }); }  function unlockandroidkey(){   //this function connects server via jquery ajax , gets json string inside success or error block   return $.ajax(...).then(function(data) {        return something;   }); } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -