简体   繁体   English

Ajax无法使用.htaccess`重写url

[英]Ajax not working with .htaccess`rewrite url

I create the basic project and use htaccess rewrite url and jquery ajax 我创建基本项目并使用htaccess重写url和jquery ajax
.htaccess: 的.htaccess:

Options +FollowSymLinks  
    RewriteEngine On  
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f      
    RewriteRule ^(.*)$ index.php?param=$1

gallery.php : gallery.php:

<script type="text/javascript">                
    window.onload = function() {

        $.ajax({
            type: "POST",
            url: "/test1/controller.php",
            data: {brand:"<?php echo $id;?>"},
            dataType: "json",
            success: function (data) {
                alert(data.response)
            }
        });
    }     

controller.php : controller.php:

$brand=$_REQUEST['brand'];
        $arr = array ('response'=>$brand.'___jason','comment'=>'test comment here');
        echo json_encode($arr);

but when write url in address bar of browser like: 但是当在浏览器的地址栏中写网址时,例如:

http://localhost/test1/gallery/gucci

jquery ajax not working but when write : jQuery的ajax无法正常工作,但写时:

http://localhost/test1/gallery

jquery ajax is working well. jQuery ajax运行良好。

I must use end of url (gucci) for id , but when I write it, jquery isn't working 我必须使用url(gucci)的结尾作为id,但是当我编写它时,jQuery无法正常工作

First of all... You are not telling us what framework or what software you're running there. 首先...您没有告诉我们您在哪里运行什么框架或什么软件。

Use firebug or other console addons depending on your browser, open it up, go to the console tab, refresh the page... you should see an ajax call over there and then you can click it, and view exactly what's wrong, or what errors you get, even what you send. 根据您的浏览器使用firebug或其他控制台插件,打开它,转到“控制台”选项卡,刷新页面...您应该在那看到ajax调用,然后可以单击它,并确切地查看出了什么问题或什么问题错误,甚至是发送的错误。

See this: http://screencast.com/t/5TQhLOzKq5 看到这个: http : //screencast.com/t/5TQhLOzKq5

Example in case your jQuery isn't loaded properly you will be getting a $ is undefined, in the console or js error console 在控制台或js错误控制台中,如果您的jQuery加载不正确的示例,您将得到一个未定义的$

When using relative paths for your ajax calls with url rewriting keep in mind that the directory is changing relatively to your url . 在通过URL重写对ajax调用使用相对路径时,请记住, 目录相对于url而言是相对变化的

To avoid this behavior use absolute paths or set in your html the base tag like this: 为了避免这种行为,请使用绝对路径或在html中设置基本标记,如下所示:

<base href='http://your-server-address.com'/>

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

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