2016-03-20 13 views
1

同じ関数で "mentorslist" varibaleにアクセスする方法。 "mentorslist"はajax呼び出しの成功です。しかし、私はmentors()関数内でアクセスすることができません。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 
} 

はmentorslist変数は、AJAXの成功に設定すると、カスタム関数を介してそれを返すようにしようとしているここを参照してくださいそれは私はundefinedを返します。

答えて

0

Ajaxは非同期であるため、成功イベントが発生したときにのみ変数値が返されます。だから、成功を呼び出すために、独自の関数を定義することができます

:あなたは常にsynchronus 1に非同期のAJAX呼び出しを変更するために偽の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 
     }); 

 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; 
     } 
+0

メンターリストを使用して、未定義を与える – Ni3

+0

それは働いている親愛なる...あなたはロックです..ありがとうございます。 – Ni3

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(); 
+0

スクリプトを共有できますか? – Ni3

+0

"mentorslist"変数を機能外に設定しましたが、それでも機能しません。 – Ni3

関連する問題