在Zend Framework中从Jquery调用Controller Action时出现404(找不到页面)错误

时间:2013-06-26 06:06:59

标签: php jquery ajax zend-framework

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

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)application.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();
?>

3 个答案:

答案 0 :(得分:2)

如果您在本地服务器中指定了虚拟主机,那将会容易得多。

编辑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并瞧!

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

同时检查APPLICATION_ENV是否为开发而非生产。否则它不会显示错误。

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

相关问题