0

我想通过 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 错误。

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

4

1 回答 1

0

Using JavaScript you can only .load() URLs that you would normally type in your browser's address bar (since JS is client-side). You cannot use absolute file paths of your server this way.

To load this file, you have basically the following options:

  1. Place the file above your server's DocumentRoot (i.e. most likely where your index.php file is, often public_html or www)
  2. Include it using PHP: <?php include($yourAbsolutePath); ?>

Since you want to use AJAX to load the file, the best thing you could do is .load() a PHP file that is above your DocumentRoot and include() the file from there.


Example

  1. Create a .php file in your DocumentRoot (e.g. myFile.php)
  2. Put the following myFile.php:

    <?php include('/app/design/frontend/default/mytheme/template/catalog/layer/test.phtml'); ?>
    
  3. Change your .load() line to:

    $("#showsearch").load("/myFile.php", function(response, status, xhr) { ...
    
于 2012-06-23T08:45:28.460 回答