2010-12-02 39 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 

希望这有助于

相关问题