gpt4 book ai didi

javascript - AJAX/JQuery 函数未定义

转载 作者:行者123 更新时间:2023-12-02 13:52:01 24 4
gpt4 key购买 nike

我正在开发一个允许用户邀请其他用户的项目。当用户收到邀请时,应该弹出一个窗口,嗯……弹出……要求他们接受或拒绝。为此,我使用 AJAX 调用来检查他们是否有任何邀请。这最终将是一个自动调用的函数,但现在我只是用一个简单的按钮和 onclick 函数来测试它。

所发生的情况是,AJAX 请求转到 checkInvitations.php,它检查充满用户的数据库表。用简单的英语来说,checkInvitations.php 检查发送过来的“用户”AJAX 是否有邀请。如果这样做,checkInvitations 会将信息传递回 AJAX 请求,其中包含(邀请用户的人员姓名)和(邀请确认)。

无论出于什么原因,尽管我已经导入了 JQuery 库,但我的函数仍然显示为未定义。我不知道为什么会这样。

这是带有 AJAX 请求的函数。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript">

function checkForInvitations()
{
var invitedPlayer = '<?php echo $_SESSION["goodUser"]; ?>' //invitedPlayer = the logged-in user
console.log("invitedPlayer is: "+invitedPlayer); //variable debug check

$.ajax({
type: "post",
url: "checkInvitations.php",
data: {invitedPlayer: invitedPlayer},

success: function(data)
{
// parse json string to javascript object
var invitations = JSON.parse(data);
console.log(invitations); //variable debug check

// check the invitations to/from
var invitingPlayer = invitations.invitationFrom;
var invitationStatus = invitations.invitationStatus;

//if player has received an invite, pop up a screen asking them to confirm/accept the invite
if(invitationStatus != 'false')
{
clearInterval(checkInvitationIntervalId);
confirm_yes = confirm(invitingPlayer+" invited you to a game. Accept ?");

}
}
})
}

这是它请求的 PHP 页面

<?php
session_start();

// Create connection
$conn = new mysqli('localhost', 'root', '', 'warzone');

// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

$invitations = array();
//look for rows in database where user=the invited user and the invitestatus has been set to true (after invite was sent)
$request = "SELECT * FROM warzone.logged_in_users WHERE USER='".$_POST["invitedPlayer"]."' AND INVITESTATUS='TRUE'";
$res = $conn->query($request);

if($row = $res->fetch_assoc())
{
$invitations["invitationFrom"]=$row["INVITING_PLAYER"];
$invitations["invitationStatus"]='true';
}
else
{
$invitations["invitationFrom"]='none';
$invitations["invitationStatus"]='false';
}

echo json_encode($invitations);

?>

请记住,当我在上面的 PHP 文件中使用 $_SESSION["goodUser"] 代替 $_POST["invitedPlayer"] 时,我得到了我正在寻找的确切输出。我知道这有效。显然,我只是无法让它与 $_POST 一起工作,因为 AJAX 请求没有被发出/被破坏/未定义。

感谢任何帮助!

最佳答案

来自Mozzila Developer API on script tags .

If a script element has a src attribute specified, it should not have a script embedded inside its tags.

因此,您希望将 jquery 的包含内容分离到单独的标记中。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript">
</script>
<!-- end script before you start the one with your code in it -->
<script type="text/javascrpt">

// Your code that involves $ here...

</script>

关于javascript - AJAX/JQuery 函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40953074/

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