Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring data returned from ajax post- should I use async:false to store in var? Is there a better way?
    primarykey
    data
    text
    <p>I have this code:</p> <pre><code> $.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'); </code></pre> <p>As you can see, I'm grabbing two bits of data - postHTML &amp; dataArray- through an ajax post function. I want to put the dataArray in a var. I've read similar questions and I beleive the solution is to use 'async: false' - causing the function to wait for the data, and insert it in the var before proceeding. So I have several questions surrounding this:</p> <ol> <li><p>Firstly, how do you set 'async: false' using the shorthand jquery $.post as I am doing above, rather than $.ajax ({ type:'POST' }) ? is it possible? This would be useful to know, but I can't work it out or find an answer anywhere.</p></li> <li><p>I've heard a lot of negative comments about using 'async: false'. Would you recommend it? If not, how can I just store some data string on the page for later use? At the moment as you can see I've set the data to be inserted into a div (set to display:none) which strikes me as being less than ideal, to say the least.</p></li> <li><p>Why does the function not wait for the data to be inserted into a var, but when you set it to insert the data to an element on the page via .html(), it works? I guess this is like opening a door for the data string to just spill into the page in it's own time..? But why can't this methodology apply to inserting data into a var- why can't the door stay open to the var as well? (If you see what I mean!).</p></li> </ol> <p>Apologies for a rather lengthy question- any answers or partial answers to the points above will be very much appreciated. Thanks.</p> <p>EDIT: here is my full code (You can probably ignore all the data gathering bits, but I'll post the lot anyway)-</p> <pre><code> $('#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'); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload