CodeIgniter Forums
json_encode view returns null - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: json_encode view returns null (/showthread.php?tid=62663)



json_encode view returns null - shardas - 08-11-2015

Dear CI Masters,

I don't know if I found a bug or I'm just don't get it.

Jquery makes a post to a CI Controller:

Code:
$.post('/update_uri',{var:val},function(return){
     console.log(return);
})

The Controller:


PHP Code:
$result['pageX'] = $this->load->view('pageX','',TRUE);
print_r($result); //This will be logged on the client
//OR
echo json_encode($result); //Empty String will be logged 

Can somebody confirm this?
Thanks in advance


RE: json_encode view returns null - gadelat - 08-13-2015

Use Network Monitor in your browser's developer tools and check if calling url is correct and how does response look like


RE: json_encode view returns null - pravins - 08-13-2015

view returns data as a string
please check "Returning views as data" section in View documentation.
http://www.codeigniter.com/user_guide/general/views.html?highlight=view#returning-views-as-data


RE: json_encode view returns null - shardas - 08-13-2015

(08-13-2015, 12:42 AM)gadelat Wrote: Use Network Monitor in your browser's developer tools and check if calling url is correct and how does response look like

Thanks for your reply. I found the issue by myself. It's because json_encode requires UTF8 string. So this works:


Code:
$result['pageX'] = utf8_encode($this->load->view('pageX','',TRUE));



RE: json_encode view returns null - InsiteFX - 08-13-2015

With jQuery you should always use the full path:

PHP Code:
<head>
<
script type="text/javascript" charset="utf-8">
    
//<![CDATA[
        
var base_url "<?php echo base_url(); ?>";
        var 
site_url "<?php echo site_url(); ?>";
    
// ]]>
</script>
</
head

Then use the base_url or site_url in you jQuery code.


RE: json_encode view returns null - shardas - 08-17-2015

(08-13-2015, 04:22 AM)InsiteFX Wrote: With jQuery you should always use the full path:


PHP Code:
<head>
<
script type="text/javascript" charset="utf-8">
 
//<![CDATA[
 
var base_url "<?php echo base_url(); ?>";
 var 
site_url "<?php echo site_url(); ?>";
 
// ]]>
</script>
</
head

Then use the base_url or site_url in you jQuery code.

Thanks for this hint. I'm going to change that in my project.