【问题标题】:Getting 404 (Page Not Found) error while calling Controller Action from Jquery in Zend Framework在 Zend Framework 中从 Jquery 调用控制器操作时出现 404(找不到页面)错误
【发布时间】:2013-06-26 06:06:59
【问题描述】:

我是 Zend-Framework 的新手,我正在尝试使用 Jquery 从 indexController.php 文件中调用操作方法,那时我收到错误:

Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost/zf_demo/public/index/process

My Code is : IndexController.php

public function processAction() {
    echo "Successfully Called processAction";
}

我正在使用以下代码调用此操作:

    $.ajax({
        type: "POST",           
        url: "http://localhost/zf_demo/public/index/process",
        success: function() {
            alert("AJAX call a success!");
        },
        error: function() {
              alert("AJAX call an epic failure");
        }
    });

.htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

更多帮助:

1) 应用程序.ini

[production]

phpSettings.display_startup_errors = 0
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1



[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

[config]
resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 
resources.db.params.dbname = pankaj_test

2) Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initController()
    {
        $this->_frontcontroller = Zend_Controller_Front::getInstance();
        $this->_frontcontroller->setControllerDirectory(APPLICATION_PATH . 'controllers/');
    }

    protected function _initRoute()
    {
        $this->_route = $this->_frontcontroller->getRouter();
        $this->_route->addRoute('default', new Zend_Controller_Router_Route(
            ':controller/:action/*', array(
                'module'     => 'default',
                'controller' => 'index',
                'action'     => 'index'
            )
        ));
    }


    public function run()
    {
        try {
            $this->_frontcontroller->dispatch();
        }
        catch (Exception $e) {
            print nl2br($e->__toString());
        }
    }

    protected function _initDb()
    {
      $configData=array(
            'database' => array(            
            'adapter' => 'Pdo_Mysql',           
            'params' => array(          
                            'host' => 'localhost',                          
                            'username' => 'root',                           
                            'password' => '',                           
                            'dbname' => 'pankaj_test')
            )
        );

        $config=new Zend_Config($configData);

        $db=Zend_Db::factory($config->database);

        //$db = Zend_Db_Table_Abstract::getDefaultAdapter();

        Zend_Db_Table::setDefaultAdapter($db);





    }

}

我的 index.php(来自公共文件夹)

<?php

// Define path to application directory

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/* Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap()
            ->run();
?>

【问题讨论】:

  • 缺少重写的 htaccess?
  • 请检查一下...我已经添加了代码。
  • 直接在浏览器中访问,而不是使用Ajax,是否有效?
  • no...当时我收到错误:在此服务器上找不到请求的 URL /zf_demo/public/index/process
  • 如果你尝试直接在浏览器中访问页面localhost/zf_demo/public/index/process,是否有效?

标签: php jquery ajax zend-framework


【解决方案1】:

如果您在本地服务器中指定一个虚拟主机会容易得多。

编辑 apache 的 httpd.conf 并添加(在文件末尾):

<VirtualHost 127.0.0.15:80>
    ServerName 127.0.0.15
    DocumentRoot "C:\zf_demo\public" // or whatever is the path to the index.php

    <Directory "C:\zf_demo\public"> // you might need to modify this part 
        DirectoryIndex index.php    // depending on Apache's version in use
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

重新启动您的服务器。

现在,更改您的 $.ajax 调用:

url: "http://localhost/zf_demo/public/index/process",

url: "/index/process",

现在运行http://127.0.0.15,瞧!

【讨论】:

    【解决方案2】:

    您需要省略“公开”一词。所以 URL 应该是:

    url: "http://localhost/zf_demo/index/process",
    

    【讨论】:

    • 在你的引导程序中注释掉 _initRoute 并检查它是否有效。
    • @Rijandael :仍然无法正常工作....还有一件事是 404 错误是 apache 而不是 zend...这有什么不同吗...??
    【解决方案3】:

    您是否禁用了视图? 如果默认启用布局,也应该禁用布局。 您可以通过在操作末尾添加退出来解决此问题。

    public function processAction() {
        echo "Successfully Called processAction";
        exit;
    }
    

    还要检查 APPLICATION_ENV 是开发而不是生产。否则不会显示错误。

    最后通过访问 http://localhost/zf_demo/public/index.php/index/process 检查您的 .htaccess 文件是否正常。如果这可行,那么 .htaccess 就有问题。

    【讨论】:

    • 正如你所说,我在 'processAction()' 的末尾添加了 'exit' ......但它仍然不起作用:(
    • 而且我在 index.php 中将 APPLICATION_ENV 从“生产”更改为“开发”
    • 为了您的更多参考,我将发布我的 index.php,请检查是否还有其他错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多