Javascript Jquery$.post()返回相同的HTML页面
我写了一个页面,这样我可以在用户之间建立友谊。但我的问题是jquery不会发布参数 我需要一些帮助来解决这个问题 我的代码:Javascript Jquery$.post()返回相同的HTML页面,javascript,php,jquery,html,post,Javascript,Php,Jquery,Html,Post,我写了一个页面,这样我可以在用户之间建立友谊。但我的问题是jquery不会发布参数 我需要一些帮助来解决这个问题 我的代码: //currentURL -> e.g: firstname.lastname $('#friendRequestButton').on("click",function(e){ if(currentURL != "" && typeof(currentURL) != "undefined"){ $.post("se
//currentURL -> e.g: firstname.lastname
$('#friendRequestButton').on("click",function(e){
if(currentURL != "" && typeof(currentURL) != "undefined"){
$.post("sendFriendRequest.php",{user1:$.cookie("user-id"),user2:currentURL})
.done(function(data) {
console.log(data); // Here I get the same page(in HTML-format).. as result in the console.
});
//$('#friendRequestButton').attr("disabled","true");
}
});
如果你需要更多的细节,请告诉我。
我的PHP代码:
<?php
if(isset($_POST['user1']) && isset($_POST['user2'])){
$user1 = $_POST['user1'];
$user2 = $_POST['user2']
die("OK!");
}else {
die("NOO!");
}
?>
在没有看到html的情况下,这只是一个猜测,但是根据元素的名称判断,您使用的是一个按钮。可能该按钮以“正常”方式提交表单,导致页面重新加载,而您看不到ajax调用的结果 如果是这种情况,您需要防止按钮按下的默认事件(在这种情况下提交表单):
像这样使用ajax请求- $.post(“sendFriendRequest.php”,{user1:$.cookie(“用户id”),user2:currentURL}),函数(数据){ 控制台日志(数据); }); 我发现了问题: 我太蠢了: 我写的文件名如下:
sendFriendRequest..php
(两点)
它应该是:sendFriendRequest.php
谢谢大家的帮助!!:很抱歉,页面没有重新加载,是的,它是一个按钮。以及“阻止”无效的事件。@Fr0zenOfficial您确定在ajax请求中调用了正确的文件吗?该文件位于最高的文件夹中,带有jquery的页面更深3个文件夹。(很抱歉英语不好)@fr0zensofficial如果调用正确的文件,您将很容易在控制台中看到。但是如果它位于web服务器的根目录中,您应该使用绝对路径来确保:
/sendFriendRequest.php
。“请求友谊”代码部分是循环的一部分吗?如果是,您将遇到重复ID的问题。1:无循环。第二:它在根目录下的一个文件夹中,我在那里写了我的sendFriendRequest.php
页面和3个文件夹。我有一个profile.php
,请求按钮在那里。结果与往常一样。它总是有一个值。首次登录时,将设置cookie。
<?php
$friends = false;
if(!$friends){
?>
<center>
<button class="submit" id="friendRequestButton">Ask for friendship</button>
<br />
<br />
<button>Send Message!</button>
</center>
<script>
$(document).ready(function(e){
$('#friendRequestButton').on("click",function(e){
console.log(e.target);
e.preventDefault();
if(currentURL != "" && typeof(currentURL) != "undefined" && $.cookie("user-id") != "" && typeof($.cookie("user-id")) != "undefined"){
$.post("sendFriendRequest.php",{user1:$.cookie("user-id"), user2:currentURL},function(data){
console.log(data);
}
);
//$('#friendRequestButton').attr("disabled","true");
}else {
console.log("NOOOOO");
}
});
});
</script>
<?php
}
?>
$('#friendRequestButton').on("click",function(e){
e.preventDefault();
// the rest of your code