codeigniter BASE_URL()无法正常工作阿贾克斯 [英] Codeigniter base_url() not working properly for ajax

查看:129
本文介绍了codeigniter BASE_URL()无法正常工作阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用BASE_URL()在AJAX从数据库中的codeigniter项目获得。鉴于BASE_URL就像 http://domainname.com 。它工作正常。如果可能我输入我的网址如 http://www.domainname.com 在地址栏中它不工作。在code是

Using base_url() in ajax for getting the from database in codeigniter project. Given base_url is like http://domainname.com. It's works fine. If may i type my url like http://www.domainname.com in address bar it's not working. The code is

$.ajax
 ({
    type: "POST",
    url: base_url+'autocomplete/get_caste_list',
    data: {religion:$('#religion').val(),'csrf_test_name': csrf_value},
     cache: false,
     success: function(html)
        {
       $("#caste").html(html);
        } 
   });

请帮助解决这个问题。 谢谢

Please help to solve this issue. Thanks

推荐答案

在我的角度来看,最好的解决办法是:

In my point of view the best solution is:

就在HTML的标题部分添加下面的脚本。

Just add the following script in header section of HTML.

<script type="text/javascript">
    var BASE_URL = "<?php echo base_url();?>";
</script>

然后在你的Ajax code使用 BASE_URL 作为一个变量。意思是:

Then in your Ajax code use BASE_URL as a variable. Means:

$.ajax
({
    type: "POST",
    url: BASE_URL+'autocomplete/get_caste_list',
    data: {religion:$('#religion').val(),'csrf_test_name': csrf_value},
    cache: false,
    success: function(html)
    {
        $("#caste").html(html);
    } 
});

使用自己的基本网址如下方式:

Use your base url as following way:

$config['base_url'] = "http://{$_SERVER['HTTP_HOST']}/";

很简单的解决方案。

Very simple solution.

这篇关于codeigniter BASE_URL()无法正常工作阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆