1

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 :

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 :

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

[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);





    }

}

My index.php(From public folder)

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

3 回答 3

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,瞧!

于 2013-09-09T10:01:38.140 回答
0

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

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

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

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

于 2013-06-27T05:39:18.700 回答
0

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

url: "http://localhost/zf_demo/index/process",
于 2013-06-26T08:47:38.730 回答