Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery how to parse json in load event
    primarykey
    data
    text
    <p>I have a little problem. I am opening jQuery dialog with fallowing code:</p> <pre><code>$('#dialog').html('&lt;img src="img/ajax-loader.gif" width="100" height="100" /&gt;'); $('#dialog').dialog({ autoOpen: true, closeOnEscape: true, modal: true, position: ['center', 'center'], title: _title, autoResize:true, width: 'auto', buttons: { "Ok": function() { $( this ).dialog( "close" ); } } }).load(url, function(data) { var arr=$.parseJSON(data); console.log('Rez' + arr['result']); console.log('result_msg' + arr['result_msg']); $.each(arr, function (index, value) { if ($.type(value) === 'object') { console.log('rid: '+ value['rid']); console.log('rnos: '+ value['rnos']); } }); $(this).dialog("option", "position", ['center', 'center'] ); }); </code></pre> <p>All works great if i am fetching html. But if i fetch json, then how i can format it and display to user inside #dialog div? </p> <p>If i use ajax call after #dialog is open, #dialog contents won't change. Is there a way to add custom success callback to <code>load</code> event? </p> <p>If i first load all content and then open dialog, it wont position at screen center.</p> <p>if i run this code:</p> <pre><code>$('#dialog').html('&lt;img src="img/ajax-loader.gif" width="100" height="100" /&gt;'); $('#dialog').dialog({ autoOpen: true, closeOnEscape: true, modal: true, position: ['center', 'center'], title: _title, autoResize:true, width: 'auto', buttons: { "Ok": function() { $( this ).dialog( "close" ); } } }); $.ajax({ dataType : 'json', type: "GET", url: url, success: function(server_response){ console.log(server_response); $('#dialog').html('vv'); // &lt;---- even this doesn't shows switch(server_response.result) { case 'choose': var linki = ''; $.each(server_response, function (index, value) { if ($.type(value) === 'object') { linki += '&lt;a href="javascript:;"&gt;'+ value['rnos'] +'&lt;/a&gt;'; //console.log('rid: '+ value['rid']); //console.log('rnos: '+ value['rnos']); } }); console.log('Loading HTML: ' + linki); $('#dialog').html(linki); break; default: $('#dialog').html(server_response.result_msg); } $('#dialog').dialog("option", "position", ['center', 'center'] ); } }); </code></pre> <p>then #dialog content wont change.</p> <p><strong>=== Edit ===</strong></p> <p>Here is modified <code>load</code> event that accomplish what i need, but i am not sure if it is good way.</p> <pre><code>$('#dialog').dialog({...}).load(url, function(data) { var arr=$.parseJSON(data); $(this).html('&lt;img src="img/ajax-loader.gif" width="100" height="100" /&gt;'); console.log('Rez' + arr['result']); console.log('result_msg' + arr['result_msg']); switch(arr.result) { case 'choose': var linki = ''; $.each(arr, function (index, value) { if ($.type(value) === 'object') { linki += '&lt;a href="javascript:;"&gt;'+ value['rnos'] +'&lt;/a&gt;&lt;br /&gt;'; //console.log('rid: '+ value['rid']); //console.log('rnos: '+ value['rnos']); } }); console.log('Ielādējam HTML: ' + linki); $(this).html(linki); break; default: $(this).html(arr.result_msg); } $(this).dialog("option", "position", ['center', 'center'] ); }) </code></pre>
    singulars
    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.
 

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