【问题标题】:Ajax 500 (Internal Server Error) CodeigniterAjax 500(内部服务器错误)Codeigniter
【发布时间】:2015-11-03 11:09:07
【问题描述】:

我的 AJAX 代码有一些问题。我的任务是用codeigniter创建一些依赖的下拉选择框,所以我使用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>

这是我的控制器:

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

这是我在控制台的错误:

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?

【问题讨论】:

  • 你的标题暗示了一个 500 apache 错误,这个错误不是很具体。您收到任何错误消息吗?
  • 打开错误报告,看看真正的问题是什么。将您的环境更改为开发define('ENVIRONMENT', 'development');
  • 当您可以将 unit_id 作为获取/发布数据传递时,为什么要直接在您的 url 中传递它?
  • @TintuCRaju 没有错误
  • @madalinivascu 因为unit_id 是我从模型中获取的参数

标签: php jquery ajax codeigniter


【解决方案1】:

您的函数需要一个参数,但是除非您的变量 unit_id 包含斜杠 /,否则您无法正确发送它。

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

由于您使用的是POST,因此无需将其作为 url 参数发送。

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);
   }
});

从PHP端,你可以通过$_POST访问内容。

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

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2012-05-01
    • 1970-01-01
    相关资源
    最近更新 更多