【问题标题】:Ajax success function unable to access within custom function无法在自定义函数中访问 Ajax 成功函数
【发布时间】:2016-03-20 14:23:03
【问题描述】:

如何在同一函数中访问“mentorslist”变量。 “mentorslist”是ajax调用的成功。但我无法在导师()函数中访问它。

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
}

看到这里的导师列表变量设置为 ajax 成功并试图通过自定义函数返回它,但它返回我未定义。

【问题讨论】:

    标签: javascript php jquery ajax codeigniter


    【解决方案1】:

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

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

          function mentors(){
                var mentorslist = '';
                $.ajax({
                    async: false,
                    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;
          }
    

    【讨论】:

      【解决方案2】:

      它们不在同一个范围内。你不能那样做。

      你可以做的是在函数外部设置一个变量,并在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();
      

      【讨论】:

      • 可以分享脚本吗?
      • 我在函数外部设置了“mentorslist”变量,但仍然无法正常工作。
      猜你喜欢
      • 1970-01-01
      • 2017-01-24
      • 2015-03-18
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多