【发布时间】:2017-08-22 10:50:31
【问题描述】:
我正在使用 codeigniter 的 MVC,我正在尝试使用 Ajax 从我的控制器文件中加载一个函数。
我的 Ajax 代码在没有 codeigniter MVC 的情况下完美地加载了 php 文件。但是在 codeigniter 中,我不确定如何在 Ajax 函数中调用 php 文件/函数。
我的代码看起来像这样没有codeigniter,它工作正常
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
////////////////////////////////////////////
//////// This loads find without MVC////////
////////////////////////////////////////////
xmlhttp.open("GET","ajax-php.php?q="+str,true);
xmlhttp.send();
////////////////////////////////////////////
//////// This loads find without MVC////////
////////////////////////////////////////////
}
}
</script>
但是在codeigniter的视图文件中不起作用如下图
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////
xmlhttp.open("GET","<?= base_url(); ?>" + "/controller_page/ajax_file.php?q="+str,true);
xmlhttp.send();
////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////
}
}
</script>
所以问题出在下面的代码中,(已从上面的代码中删除以使其更易于阅读)因为我不确定如何在 codeigniter 的 MVC 环境中加载脚本中的函数
////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////
xmlhttp.open("GET","<?= base_url(); ?>" + "/controller_page/ajax_file.php?q="+str,true);
xmlhttp.send();
////////////////////////////////////////////
//////// this is problem////////////////////
////////////////////////////////////////////
提前致谢
【问题讨论】:
标签: php jquery ajax codeigniter