当url目录改变时,Ajax不向php发送请求

Ajax not sending request to php when url directory changes

本文关键字:php 不向 请求 Ajax url 改变      更新时间:2023-09-26

现在是真的,这个查询没有被问到,最后我放弃了,因为我找不到一个可切换的答案,我的查询。
在我的apache2服务器上,我已经启用了重写规则在。htaccess文件的根目录,以持久地保持指向所有的目录和文件到某个php文件,这工作完美,但问题是ajax脚本失败时,url改变它的目录。

这是php脚本测试[…]

<?php
//now if run the test using the root directory, i get clear response,
//but as i have told u, i have enabled apache2 module and added rewrite rules to .htaccess,
//meaning that no matter hat the url is, the url will keep pointing to the same file but the problem
// is that the ajax script does not fire p the request / send.
//i dont jnow what i am doing wrong...
if(isset($_GET['test_changes'])){
    echo 'the request has now reached the server';
    exit;
}
?>

ajax脚本

<script>
    //my ajax script is like this
    //the url seen below is in the root directory.
    //So briefly,how would i keep this url pointing to the same php file even if the directory changes
    $.ajax({
        url:'test.php?test_changes=123456789',
        cache:false,
        async:true,
        method:'GET',
        success:function(response){
            console.log(response)
        },
        error:function(error){
            console.log(error)
        }
    })
</script>

请使用绝对url来完成任务…见下面的代码

<?php

//it would still received the request
if(isset($_GET['test_changes'])){
    echo 'the request has now reached the server';
    exit;
}
?>



<script>
    //i had a little trouble thinking this through but i got it working after sometime
    //Now You need you ajax script url to be absolute to achieve something like that
    //by prepending a forward slash character to your URL
    $.ajax({
        url:'/test.php?test_changes=123456789',
        cache:false,
        async:true,
        method:'GET',
        success:function(response){
            console.log(response)
        },
        error:function(error){
            console.log(error)
        }
    })
</script>