Ajax找不到网址 [英] Ajax not finding url
问题描述
我一直在研究的一个简短问题,但似乎无法解决.
Quick question I have been working at but can't seem to get it fixed.
我有一个ajax调用,但是它没有从php文件中得到响应,它似乎正在加载404.
I have a ajax call but it is not getting a response from the php file, it seems to be loading a 404 instead.
地址栏中的网址为"mydomain.com/checkcity/"
The url in the address bar is "mydomain.com/checkcity/"
带有ajax的文件的位置是"/php/advert/script.php"
The location of the file with the ajax is "/php/advert/script.php"
要调用的php文件的位置为"/php/advert/available.php"
The location of the php file to be called is "/php/advert/available.php"
我通过htaccess使用虚拟网址.
I am using virtual urls through the use of htaccess.
这是我的ajax调用:
Here is my ajax call:
$.ajax({
type: "POST",
url: "/available.php",
data: "city="+city,
success: function(response){
alert(response);
}
});
这是我的htaccess文件:
Here is my htaccess file:
<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调用.
One thing I noticed is when I search the url in the address bar it will load the php file just not with the ajax call.
我目前正在Apache 2.2本地主机上运行它
I am running this currently on Apache 2.2 localhost
谢谢您的建议.
推荐答案
我假设您在浏览器的错误日志/控制台中看到了 404
错误消息?它还应该显示正在返回 404
响应的 URL
,根据您的请求,该响应将是: http://www.mysite.com/available.php
.
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
.
问题是您的ajax调用在 URL
参数的开头包含一个/
.这表示网站的根域(即,它指向 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",
解决方案
只需将url参数更改为以下正确 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);
}
});
这篇关于Ajax找不到网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!