我有一个Ajax调用,但它没有从php文件获得响应,而是显示404。地址栏中的url是“mydomain.com/checkCity/”,使用Ajax的文件位置是“/php/adt/script.php”,要调用的php文件位置是“/php/adt/witable.php”,我通过使用htaccess使用虚拟URL。这里是我的Ajax调用:
$.ajax({
type: "POST",
url: "/available.php",
data: "city="+city,
success: function(response){
alert(response);
}
});
这是我的htaccess文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
我注意到的一件事是,当我在地址栏中搜索url时,它将加载php文件,而不是使用Ajax调用。我目前正在Apache2.2本地主机上运行这个程序,谢谢您的建议。
I'm assuming that you're seeing the 404
error message in the error log/console for your browser? It should also be displaying the URL
that is returning the 404
response which, given your request, would be: http://www.mysite.com/available.php
.
The problem is that your ajax call contains a /
at the start of the URL
parameter. This signifies the root domain of the website (i.e. it points to http://www.mysite.com/available.php
).
url: "/available.php",
Simply change the url parameter to one of the following correct URL
s
url: "http://www.mysite.com/php/advert/available.php",
OR
url: "/php/advert/available.php",
OR
url: "available.php",
OR
url: "./available.php",
$.ajax({
type: "POST",
url: "/php/advert/available.php",
data: "city="+city,
success: function(response){
alert(response);
}
});
Ok so I know this is outdated but I thought I would answer why this was happening. I moved on to other things and this was the last thing to do so I was forced to figure it out which actually only took about half hour.
Ajax wasn't reaching the url in chrome but would in firefox. I then found out that "adblock" was blocking the url - This was because the url had "advert" in it and as soon as I relocated and renamed the file, presto it worked.
So just in case someone finds themselves in a similar situation.
Thanks