我有一个htaccess文件(Apache服务器),并重写以下url:
RewriteRule questionnaire/adminpassword questionnaire/change_admin_password.php
在javascript文件中,我执行了如下Ajax调用:
$.ajax({
type: 'POST',
cache:false,
url: 'change_admin_password.php',
data: {'currentPassword':currentPassword, 'newPassword': newPassword},
dataType: 'html',
success:function(response){
alert(response);
},
error:function(){
alert("error");
}
});
我将Ajax调用中的url更改为:url: 'adminpassword',
但是现在这个调用会重新加载页面获取内容,而不是在服务器上运行php脚本并给出响应。我是哪里做错了吗?谢谢。
You know that if you just use 'adminpassword' you gonna get a relative path. Make sure the current path is the right one, otherwise use the full path, possibly 'questionnaire/adminpassword'
Try accessing the URL directly. Type it in the browser. What do you get? Are you getting the expected response? Then inspect the actual request made by the ajax call using a tool like chrome dev tools or firebug. Is the request hitting the right URL?
If you are hitting the right URL, the problem is probably in the server.
I know this is old but I think I know what is happening here and this might help others in future
When you use questionnaire/adminpassword
your .htaccess file translates it to questionnaire/change_admin_password.php
and am guessing you are redirecting all request hitting your site to an index.php file in your root folder which processes url and requires the particular file adding to it the header and footer, its no different when ajax requests are made.
To avoid redirecting to the index file that processes the file you can avoid this totally by allowing direct access to a particular folder in your .htaccess file like so
Rewritecond %{REQUEST_FILENAME} !/api
where api is the folder those scripts called by javascript are located