【问题标题】:JQuery won't load clear url?JQuery 不会加载明确的网址?
【发布时间】:2016-02-06 05:00:30
【问题描述】:

我想加载简单的 codeignitier 控制器,但没有成功我得到 404 错误。

看起来 jquery 加载不支持漂亮/清晰的 url?有可能吗?如何实现?

当我尝试使用相同的代码加载一些文件时,它工作得很好,但是当我尝试加载 codeignitier 控制器功能时,我在控制台中得到 404。

<script type="text/javascript">
    $(document).ready(function () {
       $("#season").load('https://www.example.co/index.php/system/example');
       //i already tried without 'index.php'
    });
</script>

当我使用一些简单的文件检查加载功能是否正常工作时,它工作正常:

<script type="text/javascript">
    $(document).ready(function () {
       $("#season").load('https://www.example.co/test.php');
    });
</script>

和控制器:

public function example(){
    echo "EXAMPLE";
}

此链接存在且不是404(当然没有例子):

https://www.example.co/index.php/system/example

【问题讨论】:

  • 你检查过你的路线
  • @Snm 路由正确。我也尝试使用 base_url 函数,结果是一样的。是否可以通过加载功能加载漂亮的链接?我知道“丑陋”的网址有效,但如何使用漂亮的链接?
  • 你能告诉我你是如何使用 htaccess 和路由的吗
  • @Snm 我在 .htaccess 中重写 index.php。 / => public_html => application => index.php /controllers => System.php / => public_html => application => index.php /views => test.php 从 test.php 我调用 System.php 函数示例( )。

标签: php jquery ajax codeigniter


【解决方案1】:

尝试将其传递为

window.location.href = '<?php echo base_url('controller/method'); ?>'

【讨论】:

  • 你能给我完整的例子吗,我怎么称呼它?当我尝试它时,它会将我重定向到 /system/example
  • 系统是你的控制器,例子是方法吗?
  • window.location.href = '&lt;?php echo base_url('system/example'); ?&gt;';
  • 我已经尝试过了,它会将我重定向到系统/示例。我想在系统/测试中加载系统/示例,而不是重定向。
  • 我认为这样做就像你可以使用 ajax 并将其结果集到 div 的 html 中
【解决方案2】:

我通过添加“?”来解决它在 index.php 之前。 所以这是结果:

<script type="text/javascript">
    var PI = {

    onReady: function() {
        $.ajax({ 
        type: 'GET', 
        url: '/index.php?/system/getseasons/SC0', 
        success: function (response) { 
            alert('success');
            $("#season").html(response);
        },
        error: function(xhr, textStatus, error){
              console.log(xhr.statusText);
              console.log(textStatus);
              console.log(error);
        }


        })
    },

    };

    $( document ).ready( PI.onReady );

</script>

【讨论】:

  • 看起来您的重写规则不正确,并没有删除?。看看the suggested format 并将其与您赢得的.htaccess 进行比较。但是在此之前,请尝试直接进入浏览器时是否需要相同的url。
猜你喜欢
  • 2015-08-20
  • 2020-04-01
  • 2020-05-12
  • 2017-05-07
  • 1970-01-01
  • 2012-05-08
  • 2020-07-21
  • 1970-01-01
  • 2023-02-25
相关资源
最近更新 更多