简体   繁体   English

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

[英]Getting 404 (Page Not Found) error while calling Controller Action from Jquery in Zend Framework

I'm new to Zend-Framework and i'm trying to call action method from indexController.php file using Jquery, that time i'm getting Error : 我是Zend-Framework的新手,我正尝试使用Jquery从indexController.php文件调用操作方法,那时候我遇到了Error:

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";
}

And I'm calling this action using following Code : 我使用以下代码调用此操作:

    $.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 File : .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]

For More Help : 如需更多帮助:

1 ) application.ini 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 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);





    }

}

My index.php(From public folder) 我的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();
?>

It would be so much easier if you specified a virtual host in your local server. 如果在本地服务器中指定虚拟主机,则将非常容易。

Edit apache's httpd.conf and add (at the end of file): 编辑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>

RESTART YOUR SERVER. 重新启动服务器。

Now, change in your $.ajax call: 现在,更改您的$ .ajax调用:

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

to

url: "/index/process",

Now run http://127.0.0.15 and voila! 现在运行http://127.0.0.15 ,瞧!

You need to omit the word 'public'. 您需要省略“公共”一词。 So the URL should be: 因此,URL应该是:

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

Have you disabled the view? 您禁用了视图吗? Also layout should be disabled, if its enabled by default. 如果默认启用布局,则也应禁用布局。 You can work around this by adding exit to the end of your action. 您可以通过在操作结束时添加退出来解决此问题。

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

Also check that APPLICATION_ENV is development not production. 还要检查APPLICATION_ENV是不是开发产品。 Otherwise it will not display errors. 否则它将不会显示错误。

Finally check that your .htaccess file is ok by visiting http://localhost/zf_demo/public/index.php/index/process . 最后,通过访问http://localhost/zf_demo/public/index.php/index/process来检查.htaccess文件是否正常。 if this works than .htaccess has a problem. 如果这行得通,那么.htaccess有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM