开发者

Very strange behaviour jQuery load() with CodeIgniter

I really can't work out what's going on here. I'm using the jQuery load() method to replace some HTML within a page. In this case it is to replace the current message with one selected from a list by the user. It's all working as expected except for one vital thing. When the HTML is returned it has added the word "ARRAY" before anything else. There is no other info (like you would get from var_dump()) and no tags. Literally, just the word in capital letters! If I load the view without using AJAX then it renders as expected. The only difference is that the request was made with AJAX. Here's the relevant code:

js: (all wrapped in ready())

$('.message_summary').click(function() {
        // get message id
        id = $(this).attr('id');

        // load the new message first so that user can get on with reading it
        $('#message_window').load($.baseurl+'messages/get_message/'+id);

        // mark as read
        $.get($.baseurl+'messages/ajax/mark_as_read/'+id);

        // get current tag id
        $.get($.baseurl+'m开发者_如何转开发essages/get_current_tag', 
            function(data){
                            // get the message count
                            $.get($.baseurl+'messages/ajax/set_new_message_count/'+data,
                                    function(msg_count)
                                    { 
                                        $('.new_message_count_'+data).html(msg_count);
                                    }
                                );
            });           
});

CI controller:

 public function get_message($message_id=false)
    {
        if($message_id)
        {               
            // get an individual message and prepare for display
            $message = $this->messages_m->get_single_message($message_id);

            foreach($this->tags['id'] as $index => $tag)
            { 
                $tag_options[$tag] = $this->tags['tag_name'][$index];
            }


            $main_message = array(
                                    'message_id'    => $message_id,
                                    'tag_name'      => $message->tag_name,
                                    'tag_options'   => $tag_options,
                                    'sender' => $message->first_name.' '.$message->last_name,
                                    'date_received' => date($this->config->item('date_time_format'), $message->created_at),
                                    'subject' => $message->subject,
                                    'content' => $message->message
                                );                                    
            if($this->input->is_ajax_request())
            {
                return $this->load->view('partials/message_view', $main_message);
            }
            else
            {
                return $this->load->view('partials/message_view', $main_message, true);
            }
        }

The view file is just HTML with some php variables.

Anyone got any ideas?!?!


I'm going to try to help you debug in an answer instead of buried in comments.

We've tried if($this->input->is_ajax_request()) exit('Hello'); as the first line of get_message() and the output was Arrayhello. This leads me to believe that somewhere, you are checking for an AJAX request and echoing some array, as this is what will happen if you try to do that.

My first advice: Go back to your __construct() in the same Controller and do the same thing. If you still get the same result, go back even further, perhaps to a library that is being used. If you don't see Array anymore, start using the exit code a little further down the road until you find a point where it seems to trigger. FYI since you said you are new to the concept of debugging code, this is not the way to debug, it is just one way.

Also, it seems from your comment you get Array instead of the uppercase version now? That's weird, it doesn't sound right. Make sure you are reading the actual response in Firebug instead of what is visible on screen in your page.

Aside: A little convenience tip (something I do): Set $this->input->is_ajax_request() as a constant like IS_XHR or AJAX somewhere like in your index, constants file, or config. It just makes it easier to work with. Will the request change from ajax to non-ajax in one request? No. Just check out the source code for it in system/core/Input.php, it's extremely simple. Example:

define('AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');

This is literally what the Input class does, although it's a little obfuscated. Now you can just use:

 if (AJAX) do_ajax_stuff();

Just a personal preference, but I find it very helpful - and it's slightly less resources being used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜