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);
    }
 });
}

这是我班上的一种方法。cosole.loginsuccess方法正在工作,显示结果(加密文本中的新密码),但newpassword控制台中的打印$.ajax没有值。

知道是什么原因造成的。

4

2 回答 2

0

控制台日志记录在 ajax 成功之前执行,如果您想在设置密码后使用密码,只需将其作为参数传递给成功回调中的函数

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);
}
于 2013-11-13T10:46:32.687 回答
0

您的 console.log 在 ajax 成功之前正在运行。要检查变量,请将其分配给全局范围(仅用于测试):

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);
}

然后你可以在浏览器控制台中输入:

window.newpassword

如果您想通知用户密码,请在success方法中进行,而不是在外部进行!

于 2013-11-13T10:41:35.040 回答