Ajax无法使用.htaccess`rewrite url

时间:2022-08-10 10:51:10

I create the basic project and use htaccess rewrite url and jquery ajax
.htaccess:

我创建基本项目并使用htaccess重写url和jquery ajax .htaccess:

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

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 :

$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:

但是当在浏览器的地址栏中写入url时:

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不起作用

2 个解决方案

#1


0  

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

例如,如果你的jQuery没有正确加载,你将在控制台或js错误控制台中获得$ undefined

#2


0  

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'/>

#1


0  

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

例如,如果你的jQuery没有正确加载,你将在控制台或js错误控制台中获得$ undefined

#2


0  

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'/>