Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to access JSON fields in a AJAX call
    primarykey
    data
    text
    <p>I'm new to Php/Jquery Programming, and i past all this day trying figuring how to access to a JSON field from a Jquery AJAX call:</p> <p>I'm using codeingniter, and in my controller the related code is:</p> <pre><code>if( $this-&gt;input-&gt;is_ajax_request()) { $view_data['test'] = 'ajax'; echo json_encode( $view_data ); } else { $view_data['test'] = 'not ajax'; $data = array( 'javascripts' =&gt; array( 'js/jquery.dataTables.js', 'js/zona/dataTable_zone.js', 'js/zona/gestisci_zone.js', 'js/jquery-ui-1.8.20.custom.min.js' , 'js/jquery.ui.error-panel.js', 'js/jquery.validate.min.js' , 'js/localization/messages_it.js' ), 'style_sheets' =&gt; array( 'css/south-street/jquery-ui-1.8.20.custom.css' =&gt; 'screen', 'css/jquery.dataTables.css' =&gt; 'screen' ), 'content' =&gt; $this-&gt;load-&gt;view( 'zona/gestisci_zone', $view_data, TRUE ) ); $this-&gt;load-&gt;view( $this-&gt;template, $data); } </code></pre> <p>` In the view the related code is </p> <pre><code>&lt;div id="id_test"&gt; &lt;p&gt;&lt;?php echo $test ?&gt; &lt;/p&gt;&lt;/div&gt; </code></pre> <p>and in the javascript file the related AJAX code is</p> <p>`$.ajax( { type:"POST", url:this_url, dataType: 'json', data: post_data,<br> success: function(data){</p> <pre><code> console.log(data); console.log($("id_test").empty()); $("#id_test").empty().append(data.test); }, error: function(data){ alert("error"); </code></pre> <p>);`</p> <p>in this way the AJAX output is <code>Array{"test":"ajax"}</code></p> <p>my expected behavior is that using <code>$("#id_test").empty().append(data.test);</code> the AJAX output will be just the word ajax, but it just return a blank div. I've tried to access in many modes but i didnt figured how to accomplish this. Maybe i'm missing something because of my lack of experience or i'm using a wrong approach. Any advice please? :)</p> <p>After a little of debug i've arrived to conclusion that is a plaintext / false JSON problem. it is returning just the text rapresentation of the array and not a json array. Now, how i can send the correct json array from controller? (i already forced mimetype to application/json with no effects)</p>
    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