codeigniter BASE_URL()无法正常工作阿贾克斯无法正常、工作、codeigniter、BASE_URL

2023-09-11 00:42:24 作者:演旳够情深意浓

使用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.