0
this.encryptUserObject = function(userObj){
 var newpassword = [];
 var urlPost = '';
 console.log(password);
 urlPost = 'http:mysite.com/root/custome.svc/Encrypts'+ '/?' + "Phrase=test";
 $.ajax({
    url:urlPost,
    type:"GET",
    dataType:"json",
    contentType: "application/json",
    success: function(response){

        return response;
    },
    error:function(res){
        console.log("Bad thing happend! " + res.statusText);
    }
 });
}

This is one of method in my class. cosole.log in success method is working, showing result (new password in encryption text), but the newpassword print in console $.ajax no value.

Any idea what could be causing this.

Nothing
  • 2,644
  • 11
  • 64
  • 115
  • 1
    That should not even work, it has a syntax error. Missing one quote here `urlPost = 'http:mysite.com/root/custome.svc/Encrypts+ '/?' + "Phrase=test";` after `Enrypts` – Hanky Panky Nov 13 '13 at 10:36
  • The syntax highlighting on Stack Overflow should give you a clue – George Nov 13 '13 at 10:36
  • 2
    ajax is asynchronous. Your last console.log is executed long before success handler is executed, and newpassword is set. newpassword processing shouldonly be done as part of your success handler (or done() function) – jbl Nov 13 '13 at 10:37
  • No, it was my bad typo here. Updated! – Nothing Nov 13 '13 at 10:38
  • @jbl so should I change from success() to done()? – Nothing Nov 13 '13 at 10:40
  • that's not necessary. It should just improve readability. Anyway, the key point is that newpassword will only be available in success handler (or equivalent) . The thing you must remember here is that you have to think in terms of asynchronous execution. – jbl Nov 13 '13 at 10:44
  • So how could I return that `newpassword` in my function? – Nothing Nov 13 '13 at 10:45
  • Followed this http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success, and it's working! – Nothing Nov 14 '13 at 02:11

2 Answers2

0

Your console.log is running before ajax success. To check the variable, assign it to global scope(only for testing):

this.encryptUserObject = function(userObj){
 var newpassword = [];
 var urlPost = '';
 console.log(password);
 urlPost = 'http:mysite.com/root/custome.svc/Encrypts'+ '/?' + "Phrase=test";
 $.ajax({
    url:urlPost,
    type:"GET",
    dataType:"json",
    contentType: "application/json",
    success: function(response){
        window.newpassword = response; //here is update
        console.log(response);
    },
    error:function(res){
        console.log("Bad thing happend! " + res.statusText);
    }
 });
 //console.log("this is ur new password " + newpassword);
}

Then you can type in browser console:

window.newpassword

If you want to notify user about password, do it in success method, not outside!

Feanor
  • 3,568
  • 5
  • 29
  • 49
  • I don't want to notify it to user dear! But I want to return it in my function. `return()` also in the same scope as `console.log` above. – Nothing Nov 13 '13 at 10:45
  • 1
    @Domo if your process includes some asynchronous behavior, then the whole process becomes asynchronous. You must think asynchronous, in terms of callback. Your function should be passed a handler function that you would invoke in the success handler, passing the newly obtained newpassword as argument – jbl Nov 13 '13 at 13:25
0

console logging is executed way before ajax success, if you want to use the password after it is set, just pass it as an argument to a function inside sucess callback

this.encryptUserObject = function(userObj){
 var newpassword = [];
 var urlPost = 'http:mysite.com/root/custome.svc/Encrypts'+ '/?' + "Phrase=test";
 newpassword = $.ajax({
    url:urlPost,
    type:"GET",
    dataType:"json",
    contentType: "application/json",
    success: function(response){

        return response;
    },
    error:function(res){
        console.log("Bad thing happend! " + res.statusText);
    }
 });
 console.log(newpassword);
}
john Smith
  • 17,409
  • 11
  • 76
  • 117
  • But when I use `return newpassword;`, it still got the same problem. – Nothing Nov 13 '13 at 10:55
  • where and why would you use return ? or better asked, where do you want to return it ? – john Smith Nov 13 '13 at 11:16
  • I've updated the question (added return newpassword;). I put the return because I want to take `newpassword` value to use in another place like `var newpwd = obj.encryptUserObject(_userobj);` – Nothing Nov 13 '13 at 22:23
  • You are still facing the same problem that you return it before the ajax-call is successfull, just put "return newpassword" to the success-function and youre done, cheers. ...there are thousand ways – john Smith Nov 13 '13 at 22:54
  • I added `return` in `success` but it still got the same problem. – Nothing Nov 14 '13 at 01:10
  • `console.log(newpassword);` in your answer print out : `Object { readyState=1, getResponseHeader=function(), getAllResponseHeaders=function(), more...}` and the value get from return statement still `undefined` – Nothing Nov 15 '13 at 07:25