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

标签 javascript php ajax

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

这里是 php 脚本 [testing...]

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

关于javascript - 当 url 目录更改时,Ajax 不向 php 发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40093476/

相关文章:

php - 我该如何着手创建这种性质的提交门户?

javascript - 如何将当前变量从 php Blade foreach 传递到 javascript?

php - 从 5 分钟前获取时间

javascript - 我的 ajax/jQuery 函数不适用于动态加载的分页内容

javascript - dwr addoptions 与关联数组

javascript - 尝试在需要管道的子进程中运行 git 命令

javascript - 在有状态组件中切换 bool 值

jquery - 在 header 中设置接受语言

javascript - 在带有 ejs 的 Node.js 中使用 AJAX

javascript - jQuery 多次点击处理