douke9379 2017-01-30 15:54
浏览 26
已采纳

AJAX在codeigniter上返回NaN

So I have a function on a controller on this path usercontroller(controller name/ajaxp(controller function). That look like this, nothing fancy:

public function ajaxp(){
echo "received";
}

Here is the path where I want my AJAX call to get.

I have a select box that look like this :

<label for="exampleInputEmail1">Apartament</label>
<select onchange="showUser(this.value)" name ="txtApartament1" class="form-control">
    <?php foreach($getEntry as $value) { ?>
    <option><?php echo $value->apartament ?></option>
    <?php }?>
</select>

onchange will trigger this AJAX :

<script>
function showUser(str) {
    var url = <?php base_url();?>+"usercontroller/ajaxp?q="+str;
    console.log(url);
    if (str == "") {
        document.getElementById("txtApartament1").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("txtApartament1").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET",url ,true);
        xmlhttp.send();
    }
}
</script>

But my url will be :

(index):258 GET http://localhost/adminigniter1/NaN65 404 (Not Found) 

insted of:

http://localhost/adminigniter1/usercontroller/ajaxp

And the console.log output will be:

NaN65

65 is the actual str from my select box that I want to sent to the cotroller. And the NaN part I think comes from this part :

var url = <?php base_url();?>+"usercontroller/ajaxp?q="+str;

Where I try to append the controller function path, that string is seen by javascript by NaN and doesn't return the corect URL. There is a way to fix it? (I'm quite new with javascript).

</div>
  • 写回答

2条回答 默认 最新

  • dsgs8208 2017-01-30 15:58
    关注

    Your <option> has no value.

    Try with this : <option value="<?php echo $value->apartament ?>"><?php echo $value->apartament ?></option>

    And your url definition have to be like this :

    var url = "<?php base_url();?>usercontroller/ajaxp?q="+str;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向