简体   繁体   中英

Ajax 500 (Internal Server Error) Codeigniter

I have some problems with my AJAX code. My task is to create some dependent dropdown select box with codeigniter, so I use AJAX:

<script type="text/javascript">
    $('#ruangan, #ruangan_label').hide();
    $('#unit').change(function() {
        var unit_id = $('#unit').val();
        if (unit_id != "") {
            var post_url = "getruangan" + unit_id;
            $.ajax({
                type: "POST",
                url: post_url,
                success: function(ruangan) {
                    $('#ruangan').append(ruangan);
                }
            });
            /* $('#ruangan, #ruangan_label').show(); */
        } else {
            $('#ruangan').empty();
            $('#ruangan, #ruangan_label').hide();
        }
    });
</script>

Here is my controller:

function getruangan($units)
{
    $unit = explode('|', $units);
    $unitkerja = $unit[0];
    $this->load->model('admin/laporanmodel', '', TRUE);
    echo (json_encode($this->laporanmodel->get_ruangan($unitkerja)));
}

This is my eror from console :

POST http://localhost:8080/simas/admin/laporan/getruangan023040500414964002KD%7CBPSPP 500 (Internal Server Error)
send @ jquery.min.js:6
x.extend.ajax @ jquery.min.js:6
(anonymous function) @ repdbr:250
x.event.dispatch @ jquery.min.js:5
v.handle @ jquery.min.js:5
ListPicker._handleMouseUp @ about:blank:810

I have no problems with the connection between controller and model. Can you help me?

Your function needs a parameter, however you're not sending it properly unless your variable unit_id contains a slash / .

var post_url = "getruangan" + unit_id;
// Output: getruangan55

Since you are using POST there is no need to send it as url parameter.

if(unit_id == '')
{
   $('#ruangan').empty();
   $('#ruangan, #ruangan_label').hide();

   return;
}

$.ajax(
{
   type: 'POST',
   url: 'getruangan',
   data: {'unit_id': unit_id, 'otherParam': otherValue},
   success: function(response)
   {
      $('#ruangan').append(response);
   }
});

From the PHP side, you can access the content through $_POST .

print_r($_POST); // To check the values passed
print_r($this->input->post('unit_id')); // To access a value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM