首页 > 解决方案 > 使用ajax的控制器方法调用

问题描述

我正在使用 codeigniter 框架。我正在尝试在下拉更改事件中使用 AJAX 函数调用来调用控制器方法。我的代码是:

$(document).ready(function() {
  $("body").on('change', '#assettype_id', function(e) {
    var categoryval = $('#assettype_id :selected').val();
    // assettype_id is dropdown id. On change event of 
    // dropdown, controller method will be called

    myurl = 'http://mylocalsite/index.php/controllername/controllermethod/' + $.now();

    alert("category id = " + categoryval); // for testing
    $.ajax({
      cache: false,
      type: 'POST',
      data: {
        id: categoryval
      },
      url: myurl,
      dataType: 'html',
      success: function(data1) {
        alert("inside ajax call"); // for testing
        $('#result').html("");
        // result is a div tag used to display result//
        $("#result").html(data1);
      },
      error: function(jqXHR, textStatus, errorThrown) {
        alert('error');
      },
      complete: function(xhr, status) {
        alert("The request is complete!");
      }
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

此代码对于下拉列表的初始 2 或 3 个更改事件并获得新输出非常有效,但在从下拉列表中选择 2 或 3 次后,不会像 AJAX 方法不起作用那样获得新结果。

我已经放了 2 个警报以供检查。alert带有消息'category id ='正在调用下拉列表的每个更改事件,但是alert在选择了2或3个下拉列表后,消息“inside ajax call”没有显示,甚至不会进入AJAX调用的错误部分。

我想知道这里出了什么问题?谢谢你的帮助。

标签: ajaxcodeigniter

解决方案


推荐阅读