在 AJAX 中调用 PHTML 文件


call phtml file in ajax

我想通过ajax调用catalog/layer/view.phtml文件。

代码中绝对没有变化,一切都是一样的,只是想在 ajax 中调用 view.phtml。

<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>

我将使用以下 jquery 代码。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
    $(function() {
    $("#showsearch").load("/app/design/frontend/default/mytheme/template/catalog/layer/test.phtml", function(response, status, xhr) {    alert(response);   
});
     });
</script>
<div id="showsearch"></div> 

我将jquery代码放在view.phtml中,但它返回404错误。

我是在做一些完全不可能的事情,还是可以用其他方式完成?

使用 JavaScript,您只能.load()通常在浏览器地址栏中键入的 URL(因为 JS 是客户端)。您不能以这种方式使用服务器的绝对文件路径。

要加载此文件,您基本上有以下选项:

  1. 将文件放在服务器DocumentRoot上方(即最有可能index.php文件所在的位置,通常是public_htmlwww
  2. 使用 PHP: <?php include($yourAbsolutePath); ?> 包含它

由于您想使用 AJAX 加载文件,因此您可以做的最好的事情是.load()一个高于DocumentRoot的 PHP 文件,并从那里include()该文件。


  1. DocumentRoot中创建.php文件(例如 myFile.php)
  2. 输入以下 myFile.php:

    <?php include('/app/design/frontend/default/mytheme/template/catalog/layer/test.phtml'); ?>
    
  3. .load()行更改为:

    $("#showsearch").load("/myFile.php", function(response, status, xhr) { ...