Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So. if you want to echo out a JSON obj that can be pick it up using ajax, then u need to make sure that the output mime-type of your page can serve JSON data.</p> <p>also notice that u cannt echo anything else with the json data that u want out of ur function. as i can see in your code u echo json then load a view ! u need to split that into 2 separate pages</p> <p>Fix:</p> <pre><code>public function _JSON() { $this-&gt;load-&gt;model("values"); $queryresults = $this-&gt;values-&gt;getDb(); $arr = array(); foreach($queryresults as $row) { $arr[] = $row-&gt;postcode; } return $arr; } public function json_body(){ $this-&gt;load-&gt;view('answer',array('data'=&gt;$this-&gt;_JSON())); } public function getJSON(){ $this-&gt;output -&gt;set_content_type('application/json') -&gt;set_output(json_encode($this-&gt;_JSON())); } ///note that $this-&gt;_JSON() now return ur $arr so u can use it in many methods. </code></pre> <p>now to load page u can go to <code>/json_body</code> 'rename it as u want' and js should be</p> <pre><code>&lt;script&gt; $.post('&lt;?=base_url('welcome/getJSON')?&gt;', function(data) { alert(data); }); &lt;/script&gt; &lt;!--notice that base_url show echo out ur url base on ur config.php file--&gt; </code></pre> <p>NOTE:</p> <p>function getJson get use the power of CI Output class to set the mime-type of page and make sure this array is the only output. this is not a must but its just best practice. if u want u can relapce all 3 lines with simple <code>echo json_encode($this-&gt;_JSON());</code> and it will work too !</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.
    1. VO
      singulars
      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