【问题标题】:I get 'page not found' error with Codeigniter and AJAX requestCodeigniter 和 AJAX 请求出现“找不到页面”错误
【发布时间】:2013-08-03 18:05:02
【问题描述】:

我正在使用 CodeIgniter 开发一个项目,但是在使用 jQuery AJAX 发送请求时出现问题。我的默认控制器是:

$route['default_controller'] = "test";

这是我的测试控制器:

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class test extends CI_Controller {

    function __construct() {
        parent::__construct();
    }
    public function login_with() {
        echo "1";
    }
}

这是我的 AJAX 请求:

$(function() {
    $('#login_with').click(function() {
        $.ajax({
            type: "post",
            url: "<?= base_url('test/login_with') ?>",
            data:"login=1",
            success:function(ajax_success){
                alert(ajax_success);
            }
        });
    });
});

最后是我的 .htaccess 文件:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]

这里有什么问题?发送请求时出现 404 page not found 错误。

【问题讨论】:

  • 你把你的ajax代码放在哪里了。在什么文件里??
  • 我通常只是这样做:url : '/test/login_with' 并且总是有效。

标签: .htaccess codeigniter jquery controller


【解决方案1】:

试试site_url()like

$.ajax({
        type: "post",
        url: "<?= site_url('test/login_with'); ?>",
        data:"login=1",
        success:function(ajax_success){
            alert(ajax_success);
        }
    });

并尝试在 echo 之后退出

public function login_with() {
    echo "1";
    exit;
}

【讨论】:

  • 你的控制台重定向网址是什么......??
  • @Gautam3164 login_with() 方法并不是真正的问题;)
【解决方案2】:

首先检查您的网站是否启用了mod_rewrite 然后 检查你的 config/config.php 中是否有这个

$config['index_page'] = ''

我建议你试试这个 htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

【讨论】:

  • 像= site_url();?>就不用回显了
  • 哦,好吧,总是不使用捷径:/抱歉,嗯
  • 是的,当我使用 = shot tag 时没有必要 :) 问题仍然相同。你可以在这里查看直播; pvpranks.com/uzuntweet
  • 但是老兄......pvpranks.com/uzuntweet/twitter_baglan这不是你在这里发布的内容:P @OnurŞevik
  • 是的,我已经更改了函数名称。名称现在在这里并不重要,只是为什么它不执行功能
【解决方案3】:

试试这个

$(function() {
    $('#login_with').click(function() {
        $.ajax({
            type: "post",
            url: window.location.origin+"/test/login_with",
            data:"login=1",
            success:function(ajax_success){
                alert(ajax_success);
            }
        });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2017-03-07
    • 1970-01-01
    • 2017-02-27
    • 2016-12-06
    • 2017-11-16
    相关资源
    最近更新 更多