CodeIgniter + jQuery UI autocomplete = 500 internal server errors (with code)

Here is the view code:

<html>
<head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

        <!-- Load JQuery UI -->
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
    $( function() {    

        $("#input").autocomplete({
            source: function(req, add){
                $.ajax({
                    url: '<?php echo base_url(); ?>test/ac2',
                    dataType: 'json',
                    type: 'POST',
                    //data: req,
                    data: 'input='+req,
                    success: function(data){
                        if(data.response =='true'){
                           add(data.message);
                        }
                    }
                });
        },
        minLength: 2,
        select: function(event, ui){
            $(this).end().val(ui.item.value);
            }
        });

     });      
</script>
</head>
<?php

echo form_open();
echo form_input('input', '', 'id="input"');
echo form_close();

?>
</html>

and controller code:

class Test extends CI_Controller {

    function index()
    {
        $this->load->view('vw/test_vw');
    }

    public function ac2()
    {

        //$search = $this->input->post('term');
              $search = $this->input->post('input');

        $data['response'] = 'false';

        $this->db->select('*');
        $this->db->from('loc_exercise');
        $this->db->like('locations', $search);
        $locations = $this->db->get()->result();


        if (count($locations) > 0) {
            $data['message'] = array();

            foreach ($locations as $location) {
                $data['message'][] = array(  'label' => $location->locations,
                'item'  => $location->locations,
                'value' => $location->locations );
            }

            $data['response'] = 'true';
        }
        echo json_encode($data);
    }

When I enter something into the input field, I get this on the console:

POST http://my.example.com/test/ac2 500 (Internal Server Error)

and in the CI error logs there seems to be no problem (log_threshold is 1, / logs is chmod 777).

BTW I have my config.php with query_strings TRUE and allow_get_array TRUE.

Any ideas on how to fix this?

+3
source share
4 answers

This is almost certainly a problem with the CSRF token.

Watch it on the CI forums and this blog post

+4
source

Nothing in your question suggests that you need to turn query_stringson orallow_get_array

try it

comment out this line

$search = $this->input->post('term');

$search

public function ac2($search)

URL

http://yourdomain.com/index.php/test/ac2/<insert your search string here>

, , URL-

.

...

data: 'term='+req,  //<-- change to this
+2

, csrf POST, :

    $("#input").autocomplete({
        source: function(req, add){

    var cct = $("input[name=ci_csrf_token]").val(); //  <---

            $.ajax({
                url: '<?php echo base_url(); ?>test/ac2',
                dataType: 'json',
                type: 'POST',
                //data: req,
                data: 'input='+req,
    'ci_csrf_token': cct,   //  <---
                success: function(data){
                    if(data.response =='true'){
                       add(data.message);
                    }
                }
            });
    },
    minLength: 2,
    select: function(event, ui){
        $(this).end().val(ui.item.value);
        }
    });

 }); 

:

     csrf_test_name:$("input[name=csrf_test_name]").val(),

, :

  <?php echo form_open();?>

: * http://codeigniter.com/forums/viewthread/176318/ **

+1

Test your PHP code without AJAX. Your error indicates that your PHP is the one that causes the error.

One more thing is better to see if you have results before you call ->result()

0
source

All Articles