Ajax成功函数无法在自定义函数内访问

时间:2016-03-20 14:23:04

标签: javascript php jquery ajax codeigniter

如何访问" mentorslist"同一函数中的varibale。 " mentorslist"是ajax电话的成功。但是我无法在mentors()函数中访问它。

function mentors(){
    var mentorslist = '';
    $.ajax({
      type: "POST",
      url: <?php echo  '"'.base_url().'index.php/MentorList/'.'"'; ?>,
      data: { pagelimit: 1,json: "true" },
      success: function( msg ) 
      {
          var obj = jQuery.parseJSON(msg);
          var $mentor_list ="";  
          var mlist = '';
          jQuery.each( obj.resset, function( i, val ){
            mlist = mlist+'<option value="'+val.mentor_Id+'">'+val.Name+'</option>';
            $("#mlist").get(0).options[$("#mlist").get(0).options.length] = new Option(val.Name,val.mentor_Id);
          });
         mentorslist = mlist;   //Able to access here
        }
    }); 

    return mentorslist; // gives undefine error
}

在这里看看mentorslist变量是设置成ajax成功并试图通过自定义函数返回它但它返回我未定义。

2 个答案:

答案 0 :(得分:0)

它们的范围不同。你不能这样做。

您可以做的是在函数外部设置一个变量,并在ajax调用成功时设置它的值。之后你可以玩它。

同时使ajax调用同步,以便在完成后脚本将继续。

mentorlist;
function mentors(){
    mentorslist = '';
    $.ajax({
      type: "POST",
      url: <?php echo  '"'.base_url().'index.php/MentorList/'.'"'; ?>,
      data: { pagelimit: 1,json: "true" },
      success: function( msg ) 
      {
          var obj = jQuery.parseJSON(msg);
          var $mentor_list ="";  
          var mlist = '';
          jQuery.each( obj.resset, function( i, val ){
              mlist = mlist+'<option value="'+val.mentor_Id+'">'+val.Name+'</option>';
              $("#mlist").get(0).options[$("#mlist").get(0).options.length] = new Option(val.Name,val.mentor_Id);
          });
     mentorslist = mlist;   //Able to access here
     }
    }); 
}

function asd()
{
    console.log(mentorlist);
}

mentors();
asd();

答案 1 :(得分:0)

因为Ajax是异步的,所以只有在触发成功事件时才有变量值。因此,您可以定义自己的函数来调用成功:

steps{
    shell('if [ "$my-variable" == "new" ]; then\n\
cred="new"\n\
echo "set cred to new" \n\
else\n\
cred="old"\n\
echo "set cred to old"\n\
fi;')
}

wrappers{
    credentialsBinding{
        usernamePassword('userVar', 'passwordVar', '${cred}')
    }
}

steps {
    shell(fab ${envName} start')
}

在任何情况下,您都可以将ajax调用的async属性设置为false,以便将ajax调用从异步更改为同步调用:

        var mentorslist = '';

        function mentors(myCallBack) {
            return $.ajax({
                type: "POST",
                url: "yourPHP",
                data: {pagelimit: 1, json: "true"},
                success: function(msg) {
                    myCallBack(msg);
                }
            });
        }

        mentors(function(msg) {
            var obj = jQuery.parseJSON(msg);
            var $mentor_list = "";
            var mlist = '';
            jQuery.each(obj.resset, function (i, val) {
                mlist = mlist + '<option value="' + val.mentor_Id + '">' + val.Name + '</option>';
                $("#mlist").get(0).options[$("#mlist").get(0).options.length] = new Option(val.Name, val.mentor_Id);
            });
            mentorslist = mlist;   //Able to access here
        });