jquery $.get & $.ajax , abour async & sync

You have a few different mistakes. First, $.get doesn't return the return value of the callback function. It returns the XHR object. Second, the get function isn't synchronous, it's asynchronous so showGetResult will likely return before get completes. Third, you can't return something from inside the callback to the outer scope. You can, however, bind a variable in the outer scope and set it in the callback.
To get the functionality that you want, you'll need to use $.ajax and set the async option to false. Then you can define a variable in the outer scope and assign it in the ajax callback, returning this variable from the function.

function showGetResult( name ){ 
var result = null; var scriptUrl = "somefile.php?name=" + name; 
$.ajax({ url: scriptUrl, type: 'get', dataType: 'html', async: false, success: function(data) { result = data; } }); 
return result;}

You would probably be better served, though, figuring out how to do what you want in the callback function itself rather than changing from asynchronous to synchronous calls.

The fundamental mistake you are making is that the AJAX call is made asynchronously, so by the time you return, the result is not yet ready. To make this work you could modify your code like this:

$(function() {
    showGetResult('John');
});

function showGetResult (name) {
    $.get('somefile.php', { 
        // Pass the name parameter in the data hash so that it gets properly
        // url encoded instead of concatenating it to the url.
        name: name 
    }, function(data) { 
        alert(data); 
    });
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末剃斧,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子忽你,更是在濱河造成了極大的恐慌幼东,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異根蟹,居然都是意外死亡脓杉,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門简逮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來丽已,“玉大人,你說我怎么就攤上這事买决。” “怎么了吼畏?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵督赤,是天一觀的道長。 經(jīng)常有香客問我泻蚊,道長躲舌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任性雄,我火速辦了婚禮没卸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘秒旋。我一直安慰自己约计,他們只是感情好,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布迁筛。 她就那樣靜靜地躺著煤蚌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪细卧。 梳的紋絲不亂的頭發(fā)上尉桩,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天,我揣著相機與錄音贪庙,去河邊找鬼蜘犁。 笑死,一個胖子當著我的面吹牛止邮,可吹牛的內(nèi)容都是我干的这橙。 我是一名探鬼主播,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼农尖,長吁一口氣:“原來是場噩夢啊……” “哼析恋!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起盛卡,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤助隧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體并村,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡巍实,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了哩牍。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片棚潦。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖膝昆,靈堂內(nèi)的尸體忽然破棺而出丸边,到底是詐尸還是另有隱情,我是刑警寧澤荚孵,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布妹窖,位于F島的核電站,受9級特大地震影響收叶,放射性物質(zhì)發(fā)生泄漏骄呼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一判没、第九天 我趴在偏房一處隱蔽的房頂上張望蜓萄。 院中可真熱鬧,春花似錦澄峰、人聲如沸嫉沽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽耻蛇。三九已至,卻和暖如春胞此,著一層夾襖步出監(jiān)牢的瞬間臣咖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工漱牵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留夺蛇,地道東北人。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓酣胀,卻偏偏與公主長得像刁赦,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子闻镶,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容