gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:02:02 27 4
gpt4 key购买 nike

现在这个查询确实没有被问到,最后我放弃了,因为我找不到我的查询的可切换答案。
在我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com