JQuery Ajax函数无法触发运行问题求助
问题描述
我正在开发一个使用jQuery Ajax的项目,想要实现点击指定按钮时,把服务器上accountPerks.txt文件的内容显示到页面的perksDiv段落里,但点击按钮后完全没反应。我加了错误提示代码也没生效,已经确认用的jQuery版本支持Ajax,甚至复制已知可用的代码也解决不了问题。
我的JavaScript代码
window.onload=loadDoc(); $(document).ready(function(){ // 预期触发的函数 $("#accountInfo").click(function(){ $.ajax({url: "accountPerks.txt", error: function(xhr, status, error) { var err = eval("(" + xhr.responseText + ")"); alert(err.Message); }, success: function(result){ $("#perksDiv").text(result); }}); }); }); // 以下是其他页面的函数 $(document).ready(function(){ $("#member1Info").click(function(){ $("#BandMember1").css("visibility", "visible"); }); }); $(document).ready(function(){ $("#member2Info").click(function(){ $("#BandMember2").css("visibility", "visible"); }); }); $(document).ready(function(){ $("#member3Info").click(function(){ $("#BandMember3").css("visibility", "visible"); }); }); function newAlbum(){ var node = document.createElement("li"); var textNode = document.createTextNode("aaaaallllpxas(2023)"); node.appendChild(textNode); document.getElementById("albumList").appendChild(node); $("#sneakPeak").css("visibility", "hidden"); } var getAlbum = document.getElementById("sneakPeak"); getAlbum.onclick=function(){ newAlbum(); } function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ if(xhttp.readyState == 4 && xhttp.status == 200){ document.getElementById("ExtraText").innerHTML = xhttp.responseText; } }; xhttp.open("GET", "TourInfo.txt", true); xhttp.send(); }
目标页面代码
<?php session_start(); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>CMP204 Unit Two Coursework Template</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <link rel="stylesheet" href="css/style.css"> </head> <body> <h1>User Profile</h1> <nav> <?php include_once "includes/links.php" ?> </nav> <?php echo "Welcome " . $_SESSION["accountName"] . ".<br>"; ?> <button id="accountInfo" type="button" class="btn btn-info btn-md">Click here to learn about account privileges</button> <!-- 触发Ajax的按钮 --> <p id="perksDiv"> <p> <!-- 用来显示文件内容的段落 --> <div class="container-fluid"> <div class="row"> <div class="col-sm-4"> <form action="addGigsAttended.php" method="post"> add gig attended <input type="text" name="GigToAdd"> <input type="submit"> </form> </div> <div class="col-sm-4"> <form action="editGigsAttended.php" method="post"> edit gig you want to change <input type="text" name="GigToEdit"> <br> select gig to replace it with <input type="text" name="GigToReplace"> <input type="submit"> </form> </div> <div class="col-sm-4"> <form action="deleteGigsAttended.php" method="post"> delete gig attended <input type="text" name="GigToDelete"> <input type="submit"> </form> </div> </div> </div> <p>Here are all the gigs you have attended</p> </br> <?php $servername = "lochnagar.abertay.ac.uk"; $username = "sql2100904"; $password = "ftAYZgzXz6au"; $dbname = "sql2100904"; $conn = mysqli_connect($servername, $username, $password, $dbname); $accountName = $_SESSION["accountName"]; $getGigs = mysqli_prepare($conn, "SELECT gigName FROM $accountName"); $results = mysqli_stmt_execute($getGigs); mysqli_stmt_bind_result($getGigs, $GigLocation); while (mysqli_stmt_fetch($getGigs)) { echo $GigLocation . " "; } mysqli_stmt_close($getGigs); myslqi_close($conn); ?> <!-- Bootstrap依赖脚本 --> <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <script src="javascript/script2.js"></script> </body> </html>
问题排查与修复方案
1. HTML标签闭合错误
页面中<p id="perksDiv"> <p>未正确闭合,会导致DOM元素选择异常,修正为:
<p id="perksDiv"></p>
2. Ajax错误回调逻辑错误
原代码用eval("(" + xhr.responseText + ")")解析响应,但accountPerks.txt是纯文本文件,不是JSON格式,解析会失败导致alert不触发。修正错误回调:
error: function(xhr, status, error) { alert(`请求出错:${status} - ${error}\n响应内容:${xhr.responseText}`); }
3. PHP代码拼写错误
myslqi_close($conn);中的myslqi是拼写错误,修正为:
mysqli_close($conn);
4. window.onload写法错误
window.onload=loadDoc();会直接执行函数,而非绑定事件,修正为:
window.onload = loadDoc;
5. 调试建议
- 打开浏览器开发者工具(F12),切换到Network标签,点击按钮后查看Ajax请求的状态码,排查是否存在404(文件路径错误)等问题。
- 查看Console标签,检查是否有JavaScript语法错误或DOM选择失败的提示。
- 确认
accountPerks.txt文件存在于正确路径,且服务器拥有读取权限。
内容的提问来源于stack exchange,提问作者JosephA
相关产品推荐
相关产品推荐

