• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
autoloading helper library model breake ajax json

#1
Hy, i am facing a really strange issue when using the autoload.php file.

if i use the $autoload['helper'] = array('myOwnHelper'); inside the config/autoload.php and i make a jquery ajax request, the json isn't render (no object show in console.log ).

But with the same ajax and the loading made inside the controller with $this->load->helper('myOwnHelper'); tadam, all worke fine as it must do (the problem is the same whith autoloading my own model).

Someone have see this stuff before? I had this behavior since CI2, it's not new.

The only manner i have found to bypass this it's to render the request as html, then trim the response Dodgy  which is terribly ugly and not really compatible with some jquery lib.

The other solution it's to load at each page helper and model who normally must be globally initialised.

Any help would be appreciated it's since days i try to solve this feature

ps: i have join an image of what is passing because i am not so sure of my english for the explanation Blush


Attached Files Thumbnail(s)
   
Reply

#2
Does your helper call get_instance()? If so, that's your problem.
Reply

#3
no, i don't call get_instance(), here is the code of the helper


PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    if(!
function_exists('load_js')){
        function 
load_js($scripts,$init){
            
$result "
            <script type='text/javascript'>
                var deferredJSFiles = [
                '//code.jquery.com/jquery-2.1.4.min.js',
                '//cdnjs.cloudflare.com/ajax/libs/jquery.matchHeight/0.6.0/jquery.matchHeight-min.js',"
;
            
$result .= "'".base_url('assets/js/jquery-ui.min.js')."',";
            
$result .= "'".base_url('assets/js/jquery.cookiebar.min.js')."',";
            
$result .= "'".base_url('assets/js/classie.min.js')."',";
            
$result .= "'".base_url('assets/js/jquery.mmenu.min.all.js')."',";
            
$result .= "'".base_url('assets/js/behavior.js')."'";
            
$result .= $scripts;
            
            
$result .= "];
                function downloadJSAtOnload() {
                    if (!deferredJSFiles.length) return;
                    var deferredJSFile = deferredJSFiles.shift();
                    var element = document.createElement('script');
                    element.src = deferredJSFile;
                    element.onload = element.onreadystatechange = function() { if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') downloadJSAtOnload(); };
                    document.body.appendChild(element);
                    if(deferredJSFiles.length == '0'){"
;
            
$result .= $init;
            
$result .= "}
                }
                if (window.addEventListener) window.addEventListener('load', downloadJSAtOnload, false);
                else if (window.attachEvent) window.attachEvent('onload', downloadJSAtOnload);
                else window.onload = downloadJSAtOnload;
            </script>"
;
            return 
$result;
        }
    } 


and i have the same problem when i try to auto load this model

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Menu_model extends CI_Model{
    
    public function 
menuPlace($place){
        
$res        $this->db->conn_id->prepare('SELECT * FROM `menu` WHERE  `place` = :place');
        
$res->bindParam(':place'$placePDO::PARAM_INT);
        
$res->execute();
        
$result     $res->fetchAll(PDO::FETCH_ASSOC);
        return 
$result;
    }
    
    public function 
menuRewrite($rewrite){
        
$res        $this->db->conn_id->prepare('SELECT * FROM `menu` WHERE  `rewrite` = :rewrite');
        
$res->bindParam(':rewrite'$rewritePDO::PARAM_INT);
        
$res->execute();
        
$result     $res->fetchAll(PDO::FETCH_ASSOC);
        return 
$result;
    }
    
    

Reply

#4
i have try several things, and no way, each time i make an auto loading the json is not interpreted

if i use a construct and put inside the load of helper model library, it make the same behavior

if i load them inside the function in the controller it work

worst, i am enable to load helper model library inside the ajax controller or it breake the json...i am driving nuts...oblige to make procedural stuff inside the controller


this work :
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Ajaxrequest extends CI_Controller {
    public function standard(){
        $result $return = array('ok'=>'json');
        
        $this
->output
            
->set_status_header(200)
            ->set_content_type('application/json''utf-8')
            ->set_output(json_encode($resultJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES))
            ->_display();
        exit;
    }



this make the json uninterpreted

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Ajaxrequest extends CI_Controller {
    public function 
standard(){
        
$this->load->library('show');
        
        
$result $this->show->showstandard();
        
        
        
$this->output
            
->set_status_header(200)
            ->
set_content_type('application/json''utf-8')
            ->
set_output(json_encode($resultJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES))
            ->
_display();
        exit;
    }
}


<?
php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Show {
    function 
showstandard(){
        
$return = array('ok'=>'json');
        
        return 
$result;
    }


as for the js
Code:
    $.post(
        $('#url').val()+'xhr/ajaxrequest/standard',
        {'token' : $("#hash").val()},
        function(data) {
            console.log(data);
        },
        'json'
    );


exept for this problem of json interpretation all work like a dream, really don't know what i am missing
Reply

#5
Use get_instance()->config->base_url() instead of base_url() in your helper.
Keep calm.
Reply

#6
the url is give by base_url() already, try your solution don't change nothing to my problem, this not empech te json to be not interpretated

i have the same problem which is describe here

i solve this problem by not loading the model (or library or helper) which cause the breake of the json format. when you looke at the response given when you use $this->load-> is ÿÿÿÿÿÿ{my json well formated}
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.