Welcome Guest, Not a member yet? Register   Sign In
json_encode view returns null
#1

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
Reply
#2

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

view returns data as a string
please check "Returning views as data" section in View documentation.
http://www.codeigniter.com/user_guide/ge...ws-as-data
Reply
#4

(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));
Reply
#5

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB