ajax - 使用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调用的错误部分。
我想知道这里出了什么问题?谢谢你的帮助。
解决方案
推荐阅读
- facebook - Facebook Oauth 提示并不要求提供所有范围
- lua - 为什么当键是表时lua kv对是序列
- unity3d - Unity - 启动构建的游戏.exe时,窗口每次都缩小?
- html - 如何根据父组件动态调整子组件的高度?
- ios - 在结构类上查看为 var
- html - 具有不透明度的子级不能出现在具有 z-index 的父级兄弟姐妹的下方
- ios - 从 Firebase 实时数据库中检索数据现在间歇性工作
- html - chrome 84 版中“a”标签的“href”行为是否发生了变化?
- plugins - Ansible 回调插件返回变量访问
- android - 语言更改按钮