作者: ~虎~ 時間: 2009-1-1 23:29 標題: [Closed]jQuery入面既AJAX點樣return value?
想進一步簡化jQuery個ajax到只剩read/write
所以將一個jQuery ajax object放在function入面 但個result return唔番出來
- function ajax(name, value){
- $.ajax({
- type: "POST",
- url: "ajax.php",
- data: (typeof(value) != 'undefined' ? 'set_' : '' ) + name + "=" + (typeof(value) != 'undefined' ? value : 1 ), //If value exist set it, otherwise get the value
- success: function(msg){
- alert(msg); //OK
- //return msg //not working
- },
- error: function(msg){
- alert(msg); //OK
- alert('Unable to save setting...');
- }
- });
- alert(msg); //undefined
- }
- alert( ajax('setting1') ); //undefined
Error: callback is not a function
http://docs.jquery.com/Frequentl ... _an_AJAX_request.3F
有無高手幫手解解畫
======================================
經過一輸研究後...發現就算整到都唔會做到我想要既結果
個問題無乜意思 所以Close file先
[ 本帖最後由 ~虎~ 於 2009-1-2 02:57 編輯 ]
作者: ~虎~ 時間: 2009-1-1 23:33
可能易睇少少
作者: chiefumpire 時間: 2009-1-2 00:57
httprequest 唔會即刻拎到result
如果request成功 就會call success()
作者: $$$ 時間: 2009-1-2 01:04
有冇得睇埋你ajax.php
作者: ~虎~ 時間: 2009-1-2 01:56
我知...success入面alert(msg)就睇到我想要既result
但msg呢個變數我帶唔到出去做個function既return
暫時hardcode住先炸 搞店呢個先玩PHP果邊其他野
[attach]780935[/attach]
作者: chiefumpire 時間: 2009-1-2 02:06
應該冇得 return
作者: ~虎~ 時間: 2009-1-2 02:28
照官網睇應該有吧...只係唔知個callback用法
改成咁樣就work 但係就唔再係AJAX 無意思了
[attach]780941[/attach]
作者: ~虎~ 時間: 2009-1-2 03:02
研究左成粒鐘 結論原來係無得懶
Comment左既綠色字係我想懶既地方...
除非唔用asynchronous 否則第二句比第一句會更先執行完 最後wallpaper只會係undefined
唔用asynchronous就唔再有ajax既優點
只有將個function放入success呢個callback入面先至會順次run得到
所以頂樓個問題再無意思 thanks all
[ 本帖最後由 ~虎~ 於 2009-1-2 03:03 編輯 ]

