[問題] ajax函式化回傳數值

看板Ajax作者 (美加航空公司)時間17年前 (2007/03/21 20:29), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/3 (看更多)
我是只有看一點w3schools.com的說明的ajax新手 我寫了這樣的script == function ajax(method,des,tf,send) { var text var xmlHttp=ajaxFunction(); if(xmlHttp){ xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4){ text=xmlHttp.responseText; //return txt 這裡return不出去,會return sub 而非function } } xmlHttp.open(method,des,tf); xmlHttp.send(send); return text //這裡return會變成未定義 } } function f(){ txt=ajax("GET","test.php",true,null); document.getElementById('mes').innerHTML=txt; } == 我可以確定我的ajaxFunction沒有錯誤 我從firebug可以看到我的responseText確實是我所需要的 但是我的document.getElementById('mes').innerHTML=txt; 這裡面的txt卻是undefined 看起來好像是網路傳輸會lag的關係 請問該怎麼解決這個似乎很基本的問題呢... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.224.40.247

03/22 00:19, , 1F
xmlHttp.onreadystatechange=function(){ -->CallBack耶~
03/22 00:19, 1F
文章代碼(AID): #160IL37t (Ajax)
文章代碼(AID): #160IL37t (Ajax)