javascript 带有 codeigniter 的 ajax jquery。url 不访问控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10398298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:44:13  来源:igfitidea点击:

ajax jquery with codeigniter. url doesnt access controller

phpjavascriptjqueryajaxcodeigniter

提问by Semur Nabiev

I am new to codeigniter and cannot get my ajax to work.
I am trying to make links load content into the main document on click.
I looked for instructions but could not figure it out. All works except the ajax returns alert('ERROR')message. nothing is loadded into <div id='load_here'></div>
Maybe I am missing something in config.php? Do i have to load some library for this to work? Any ideas would be helpfull //main document link

我是 codeigniter 的新手,无法让我的 ajax 工作。
我试图让链接在点击时将内容加载到主文档中。
我寻找说明,但无法弄清楚。除了 ajax 返回alert('ERROR')消息之外的所有工作。什么都没有加载到<div id='load_here'></div>
也许我在 config.php 中遗漏了什么?我是否必须加载一些库才能工作?任何想法都会有所帮助//主文档链接

<span id='reg_link_rules'>Link</span>  
<div id='load_here'></div>

// controller

// 控制器

class Register extends CI_Controller {
   public function hello()
   {
      echo 'hello';
   }
}

// jQuery

// jQuery

$(document).ready(function() {
    $('#reg_link_rules').click(function(eve){

    $.ajax({
      type: "GET", 
      url: "register/hello", 

      complete: function(data){
        $('#load_here').html(data);
    },
    error: function(){alert('error');}
    });
  });
});

I think the problem is that the ajax url does not access the controller. Z:/home/codeigniter/www/register/test this is where i think it takes me

我认为问题在于ajax url 无法访问控制器。Z:/home/codeigniter/www/register/test 这是我认为它需要我的地方

problem solved, the url needed to be http://codeigniter/index.php/register/hello

问题解决了,url需要是http://codeigniter/index.php/register/hello

回答by Robin Castlin

Try with url: "/register/hello".

尝试使用网址:“/register/hello”。

Might do the trick.

可能会成功。

Usually I do a

通常我做一个

<script type="text/javascript">
    base_url = '<?=base_url()?>';
</script>

At the beginning of my page and simply

在我的页面的开头,简单地

base_url+"register/hello"

instead

反而

That makes my ajax more reliable, even when / is incorrect.

这使我的 ajax 更加可靠,即使 / 不正确。

回答by gorelative

complete: function(data){
        $('#load_here').html(data);
    },

should be

应该

Just referencing another SO question ... Use success() or complete() in AJAX call

只是引用另一个 SO 问题......在 AJAX 调用中使用 success() 或 complete()

success: function(data){
        $('#load_here').html(data);
    },

AND

$.ajax({
  type: "GET", 

Should be

应该

unless you want your form vars to be submitted along in the URL.

除非您希望在 URL 中提交表单变量。

$.ajax({
  type: "POST", 

回答by César Iván Osorio Díaz

Hello You should add the base url to the URL Ajax.

您好 您应该将基本 url 添加到 URL Ajax。

 $.ajax({
  type: "GET", 
  url: <?php echo base_url();?>"register/hello", 

  complete: function(data){
    $('#load_here').html(data);
},

Remember enable the helper URL in the Controller!

请记住在控制器中启用帮助程序 URL!

Cheers

干杯

回答by Ahmed Al Bermawy

you need to pass the bas_url to ajax file

<script type="text/javascript">

$(document).ready(function(e) {
    var baseurl = <?php echo base_url();?>

    $('#reg_link_rules').click(function(){

        $.ajax({
            url : baseurl+'index.php/register/hello',
            data : '',
            type: "POST",
           success : function(){


            }
        })
        return false;
    })

});

</script>

回答by Joao Lucas Teta

Sometimes you have do add index.php to the string. Like so:

有时您确实将 index.php 添加到字符串中。像这样:

 $.ajax({
  type: "GET", 
  url: <?php echo base_url();?>"index.php/register/hello", 

  complete: function(data){
    $('#load_here').html(data);
},

But it's all about your config file however. It was the mistake in my case. Hope it helps.

但是,这完全取决于您的配置文件。这是我的错误。希望能帮助到你。

回答by RAJMOHAN

You need to ensure two matter

你需要确保两件事

please put your ajax part as url: ?php echo base_url();?>"register/hello", and type: "GET", and also need to check wheter it is routed the url on config/routes.php.

请把你的ajax部分作为url:?php echo base_url();?>"register/hello",然后输入:"GET",还需要检查它是否路由到config/routes.php上的url。