[問題] jQuery的Ajax 問題

看板Ajax作者時間13年前 (2012/06/19 15:51), 編輯推噓0(007)
留言7則, 2人參與, 最新討論串1/1
我的Ajax是這樣寫 $.ajax({ url: "ajax_insert_user_list.php", data: 'lid='+lid+'&auid='+auid, type: "POST", dataType :'json', success:function(data){ $.each(data,function(i,item){ $('#c_l_'+item.lid).html('('+item.count+')'); }); }, async: false }); 我想要把 success裡面的function 獨立出來,讓另外一個ajax事件也能利用 所以 我寫成這樣 function refresh(data) { $.each(data,function(i,item){ //alert(item.lid); $('#c_l_'+item.lid).html('('+item.count+')'); }); } success:refresh(data) 可是丟到Firefox看主控台卻出現以下的錯誤 data is not defined [在這個錯誤中斷] success:refresh(data), 請問該怎麼改才對 ,謝謝回答 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.126.209.219

06/19 15:54, , 1F
就寫succes:refresh就好 data會自己傳進去
06/19 15:54, 1F

06/19 15:54, , 2F
你原來寫法的意思把refresh(data)的回傳值丟給success
06/19 15:54, 2F

06/19 15:54, , 3F
06/19 15:54, 3F

06/19 16:02, , 4F
喔,會自己傳進去,那如果我 refresh 要接收兩個以上的參數
06/19 16:02, 4F

06/19 16:03, , 5F
又該怎麼寫呢
06/19 16:03, 5F

06/19 16:29, , 6F
ajax成功後會去呼叫success(data, textStatus, jqXHR)
06/19 16:29, 6F

06/19 16:30, , 7F
就照你的需求決定refresh的引數, 再丟給success就好
06/19 16:30, 7F
文章代碼(AID): #1Fu2-VU5 (Ajax)