// 方案2: 回调函数 // function getFile( callback ){ // // 1: 创建ajax对象 XMLHttpRequest // var xhr = new XMLHttpRequest(); // // 2: 打开通道 // xhr.open( \'get\', \'test.txt\', true ); // // 3: 发送请求 // xhr.send(); // // 4:监听通道 // xhr.onreadystatechange = function( res ){ // if( xhr.status === 200 && xhr.readyState === 4 ){ // //result = xhr.responseText; // callback( xhr.responseText ); // } // } // } var btn = document.querySelector( \'.btn\' ); btn.onclick = function(){ ajax( \'test.txt\', process ); } function process( data ){ alert( data ); }