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

时间:2016-10-17 18:43:43

标签: javascript php ajax

现在确实没有询问此查询,最后我放弃了我无法找到可查询的可查询答案。
在我的apache2服务器上,我已经在根目录中的.htaccess文件中启用了重写规则,以便持久地将除了目录和文件之外的所有内容指向某个特定的php文件,这非常有效,但是当url更改目录时ajax脚本失败的问题。 / p>

这里是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>

1 个答案:

答案 0 :(得分:2)

请使用绝对网址完成工作...... 见下面的代码

<?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>