2010-12-02 26 views
0

我有這樣的代碼:存儲從ajax post返回的數據 - 我應該使用async:false來存儲var?有沒有更好的辦法?

$.post('http://localhost/test_zone/index.php/blog/new_post', { image_codes: images, info: fieldData, post_body: body }, 
    function(data){ 
    if (data.success) { 
     if(data.error != ''){ 
     alert(data.error); 
      } 
     else { 
     $('#preview_wrapper').css('display','none').html(data.postHTML).show(1000); 
     $('#array_store').html(data.dataArray); 
     } 
     } 
    else { 
     alert('Sorry, an error occurred. No response from server.'); 
     } 
     } 

    ,'json'); 

正如你所看到的,我抓住兩個數據位 - postHTML & dataArray-通過Ajax POST功能。我想把dataArray放入一個var中。我讀過類似的問題,我相信解決方案是使用'async:false' - 導致函數等待數據,並在繼續之前將其插入到var中。所以,我有幾個問題圍繞這個:

  1. 首先,你如何設置「異步:假」使用速記jQuery的$。員額爲我做以上,而不是$。阿賈克斯({類型:'POST '})?可能嗎?這對知道有幫助,但我無法解決或在任何地方找到答案。

  2. 我聽到很多關於使用'async:false'的負面評論。你會推薦它嗎?如果沒有,我怎麼才能在頁面上存儲一些數據字符串供以後使用?正如你所看到的,我已經將數據設置爲插入到div中(設置爲顯示:無),這讓我覺得不夠理想,至少可以說。

  3. 爲什麼函數不會等待將數據插入到var中,而是當您將其設置爲通過.html()將數據插入到頁面上的元素時,它會起作用嗎?我想這就像爲數據字符串打開一扇門,只是在它自己的時間內流入頁面。但爲什麼這種方法不適用於將數據插入到變量中?爲什麼不能將變量插入到變量中? (如果你明白我的意思!)。

道歉的一個相當長的問題 - 任何答案或部分答案以上幾點將非常感激。謝謝。

編輯:這裏是我完整的代碼(你也許可以忽略所有的數據收集位,但無論如何,我會發布很多) -

   $('#submit').click(function(){ 
        $('#input_table').hide(1000); 
        if($('.image_info').length){ 
         var images = []; 
         $.each($('.image_info'), function(img_count) { 
          var img_code = $(this).attr('class').split(' ').slice(-1); 
          images.push('"' + img_count + '"' + ':' + '"' + img_code + '"'); 
          img_count++; 
         }); 
        images = '{' + images + '}'; 
        }else { 
         var images = 'none'; 
        } 
        var editor = CKEDITOR.instances.editor1; 
        var body = editor.getData(); 
        body = clean(body); 
        var fieldData = []; 
        var cleanValue ='';      
        $.each($('.field'), function() { 
         cleanValue = clean($(this).val()); 
         fieldData.push('"' + $(this).attr('id') + '"' + ':' + '"' + cleanValue + '"'); 
        }); 
        fieldData = '{' + fieldData + '}'; 
        $.post('http://localhost/test_zone/index.php/blog/new_post', { image_codes: images, info: fieldData, post_body: body }, 
          function(data){ 
           if (data.success) { 
            if(data.error != ''){ 
              alert(data.error); 
                 } 
            else { 
              $('#preview_wrapper').css('display','none').html(data.postHTML).show(1000); 
              $('#array_store').html(data.dataArray); 
             } 
            } 
           else { 
             alert('Sorry, an error occurred. No response from server.'); 
             } 
             } 

         ,'json'); 
        });  

回答

0

你不應該使用異步的,因爲你的瀏覽器掛起。 我與事件做到了這一點:

var evObj; 
var _EVENT_RESULT = "resultEvent"; 

// Listen to the event 
evObj.bind(_EVENT_RESULT, function(event, data){ 
    alert(data); 
} 

// Ajax-Request with event-triggering 
$.ajax({ 
    type:  "POST", 
    ... 
    success: function(data){ 
        evObj.trigger(_EVENT_RESULT, data); 
       } 
}); 

功能是不是在等待,因爲它的異步。這很好,因爲您的代碼不會停止,您的瀏覽器也不會掛起。 如果使用「.html()」,如果結果從服務器發回,則將結果插入頁面。它可能是,你只是「想」這是行得通的,因爲結果是非常緊張的。但是如果你的服務器會在10秒後發回結果,你的頁面上10秒內沒有結果 - 但是:你的gui沒有掛起。

編輯:試試這個:

var evObj; 
var _EVENT_RESULT = "resultEvent"; 

// Listen to the event 
evObj.bind(_EVENT_RESULT, function(event, data){ 
    alert(data); 
} 

$('#submit').click(function(){ 
    ... 
    else{ 
    ... 
    $.post('http://localhost/test_zone/index.php/blog/new_post', { 
     image_codes: images, 
     info: fieldData, 
     post_body: body }, 
     function(data){ 
     if (data.success){ 
      // At this point you could trigger the event 
      evObj.trigger(_EVENT_RESULT, data); 
     } 
     else{ 
      alert('Sorry, an error occurred. No response from server.'); 
     } 
     ,'json'); 
}); 
+0

是的,我想知道但結合功能 - ,我似乎無法得到這工作。我將發佈我的完整(原始)代碼作爲對原始問題的編輯。我嘗試在第一個'else'聲明中綁定函數,但是我無法使其工作......也許您可以告訴我如何在我的代碼中實現它。謝謝! – Inigo 2010-12-02 13:28:13

0

只需設置您的增值經銷商:

$.post('your_url', your_params, 
    function(data){ 
     //Validation code here... 
     window.postHTML = data.postHTML; 
     window.dataArray = data.dataArray;  
    },'json'); 

現在你有機會獲得window.postHTMLwindow.dataArray全局變量。 對於$。發佈是同步的(我不會推薦它),你可以使用$.ajaxSetup只有一次:

$.ajaxSetup({ //This will change behavior of all subsequent ajax requests!! 
    async: false 
}); 
$.post('http://localhost/test_zone/index.ph...) //Do $.post as usual 

希望這有助於

相關問題