Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript url目录更改时Ajax不向php发送请求_Javascript_Php_Ajax - Fatal编程技术网

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

Javascript url目录更改时Ajax不向php发送请求,javascript,php,ajax,Javascript,Php,Ajax,现在确实没有人问过这个问题,最后我放弃了,因为我找不到一个可切换的答案来回答我的问题。 在我的apache2服务器上,我在根目录中的.htaccess文件中启用了重写规则,以持续地将除目录和文件以外的所有内容指向某个php文件,这非常有效,但当url更改它的目录时,ajax脚本会失败 这是php脚本[测试…] <?php //now if run the test using the root directory, i get clear response, //but as i hav

现在确实没有人问过这个问题,最后我放弃了,因为我找不到一个可切换的答案来回答我的问题。
在我的apache2服务器上,我在根目录中的.htaccess文件中启用了重写规则,以持续地将除目录和文件以外的所有内容指向某个php文件,这非常有效,但当url更改它的目录时,ajax脚本会失败

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

//我的ajax脚本是这样的
//下面看到的url位于根目录中。
//简单地说,即使目录发生变化,我如何保持这个url指向同一个php文件
$.ajax({
url:'test.php?test_changes=123456789',
cache:false,
async:true,
方法:'GET',
成功:功能(响应){
console.log(响应)
},
错误:函数(错误){
console.log(错误)
}
})

请使用绝对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>

//我在想这个问题时有点困难,但过了一段时间,我就成功了
//现在,您需要将ajax脚本url设置为绝对url才能实现类似的功能
//通过在URL前加一个正斜杠字符
$.ajax({
url:'/test.php?test_changes=123456789',
cache:false,
async:true,
方法:'GET',
成功:功能(响应){
console.log(响应)
},
错误:函数(错误){
console.log(错误)
}
})

我总是忘记这是非常重要的,谢谢。。。