jQuery的ajax()函数不起作用,甚至没有被承认。

huangapple go评论81阅读模式
英文:

jquery ajax() function not working or even being acknowledged

问题

  1. 我目前正在开发一个项目使用了jqueryajax函数我只想让ajax函数在单击某个按钮时从服务器上显示一个文件中的一些文本但是当我点击按钮时它什么也没发生我尝试添加一个错误消息但似乎也不起作用我确保使用了支持ajax函数的jquery版本甚至复制粘贴了我知道有效的代码但似乎一切都不起作用
  2. 我的javascript文件
  3. window.onload=loadDoc();
  4. $(document).ready(function(){ // 应该被调用的函数
  5. $("#accountInfo").click(function(){
  6. $.ajax({url: "accountPerks.txt",
  7. error: function(xhr, status, error) {
  8. var err = eval("(" + xhr.responseText + ")");
  9. alert(err.Message);
  10. },
  11. success: function(result){
  12. $("#perksDiv").text(result);
  13. }});
  14. });
  15. });
  16. // 以下所有内容都是用于不同页面的函数
  17. $(document).ready(function(){
  18. $("#member1Info").click(function(){
  19. $("#BandMember1").css("visibility", "visible");
  20. });
  21. });
  22. $(document).ready(function(){
  23. $("#member2Info").click(function(){
  24. $("#BandMember2").css("visibility", "visible");
  25. });
  26. });
  27. $(document).ready(function(){
  28. $("#member3Info").click(function(){
  29. $("#BandMember3").css("visibility", "visible");
  30. });
  31. });
  32. function newAlbum(){
  33. var node = document.createElement("li");
  34. var textNode = document.createTextNode("aaaaallllpxas(2023)");
  35. node.appendChild(textNode);
  36. document.getElementById("albumList").appendChild(node);
  37. $("#sneakPeak").css("visibility", "hidden");
  38. }
  39. var getAlbum = document.getElementById("sneakPeak");
  40. getAlbum.onclick=function(){
  41. newAlbum();
  42. }
  43. function loadDoc() {
  44. var xhttp = new XMLHttpRequest();
  45. xhttp.onreadystatechange = function(){
  46. if(xhttp.readyState == 4 && xhttp.status == 200){
  47. document.getElementById("ExtraText").innerHTML = xhttp.responseText;
  48. }
  49. };
  50. xhttp.open("GET", "TourInfo.txt", true);
  51. xhttp.send();
  52. }

我想要文本文件显示在的页面

  1. <?php
  2. session_start();
  3. ?>
  4. <!doctype html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="utf-8">
  8. <title>CMP204 Unit Two Coursework Template</title>
  9. <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">
  10. <link rel="stylesheet" href="css/style.css">
  11. </head>
  12. <body>
  13. <h1>User Profile</h1>
  14. <nav>
  15. <?php include_once "includes/links.php" ?>
  16. </nav>
  17. <?php
  18. echo "Welcome " . $_SESSION["accountName"] . ".<br>";
  19. ?>
  20. <button id="accountInfo" type="button" class="btn btn-info btn-md">Click here to learn about account privileges</button> <!-- 这个按钮应该显示来自服务器的文本 -->
  21. <p id="perksDiv"> <p> <!-- 服务器的文本应该显示在这个段落里 -->
  22. <div class="container-fluid">
  23. <div class="row">
  24. <div class="col-sm-4">
  25. <form action="addGigsAttended.php" method="post">
  26. add gig attended
  27. <input type="text" name="GigToAdd">
  28. <input type="submit">
  29. </form>
  30. </div>
  31. <div class="col-sm-4">
  32. <form action="editGigsAttended.php" method="post">
  33. edit gig you want to change
  34. <input type="text" name="GigToEdit">
  35. <br>
  36. select gig to replace it with
  37. <input type="text" name="GigToReplace">
  38. <input type="submit">
  39. </form>
  40. </div>
  41. <div class="col-sm-4">
  42. <form action="deleteGigsAttended.php" method="post">
  43. delete gig attended
  44. <input type="text" name="GigToDelete">
  45. <input type="submit">
  46. </form>
  47. </div>
  48. </div>
  49. </div>
  50. <p>Here are all the gigs you have attended</p>
  51. </br>
  52. <?php
  53. $servername = "lochnagar.abertay.ac.uk";
  54. $username = "sql2100904";
  55. $password = "ftAYZgzXz6au";
  56. $dbname = "sql2100904";
  57. $conn = mysqli_connect($servername, $username, $password, $dbname);
  58. $accountName = $_SESSION["accountName"];
  59. $getGigs = mysqli_prepare($conn, "SELECT gigName FROM $accountName");
  60. $results = mysqli_stmt_execute($getGigs);
  61. mysqli_stmt_bind_result($getGigs, $GigLocation);
  62. while (mysqli_stmt_fetch($getGigs)) {
  63. echo $GigLocation . " ";
  64. }
  65. mysqli_stmt_close($getGigs);
  66. myslqi_close($conn);
  67. ?>
  68. <!--这些脚本是Bootstrap所必需的,必须放在关闭body标签之前-->
  69. <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
  70. <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  71. <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  72. <script src="javascript/script2.js"></script>
  73. </body>
  74. </html>
英文:

I'm currently developing a project which uses the jquery ajax function. I just want the ajax function to display some text from a file on a server when a certain button is clicked but when I click on the button it does nothing. I've tried adding in an error message but that doesn't seem to be working either. I've made sure to use a version of jquery which supports the ajax function and I've even copied and pasted code from sources that I know work but nothing seems to be working.

my java script file

  1. window.onload=loadDoc();
  2. $(document).ready(function(){ // the function that should be getting called
  3. $(&quot;#accountInfo&quot;).click(function(){
  4. $.ajax({url: &quot;accountPerks.txt&quot;,
  5. error: function(xhr, status, error) {
  6. var err = eval(&quot;(&quot; + xhr.responseText + &quot;)&quot;);
  7. alert(err.Message);
  8. },
  9. success: function(result){
  10. $(&quot;#perksDiv&quot;).text(result);
  11. }});
  12. });
  13. });
  14. //everything below here are functions for separate pages
  15. $(document).ready(function(){
  16. $(&quot;#member1Info&quot;).click(function(){
  17. $(&quot;#BandMember1&quot;).css(&quot;visibility&quot;, &quot;visible&quot;);
  18. });
  19. });
  20. $(document).ready(function(){
  21. $(&quot;#member2Info&quot;).click(function(){
  22. $(&quot;#BandMember2&quot;).css(&quot;visibility&quot;, &quot;visible&quot;);
  23. });
  24. });
  25. $(document).ready(function(){
  26. $(&quot;#member3Info&quot;).click(function(){
  27. $(&quot;#BandMember3&quot;).css(&quot;visibility&quot;, &quot;visible&quot;);
  28. });
  29. });
  30. function newAlbum(){
  31. var node = document.createElement(&quot;li&quot;);
  32. var textNode = document.createTextNode(&quot;aaaaallllpxas(2023)&quot;);
  33. node.appendChild(textNode);
  34. document.getElementById(&quot;albumList&quot;).appendChild(node);
  35. $(&quot;#sneakPeak&quot;).css(&quot;visibility&quot;, &quot;hidden&quot;);
  36. }
  37. var getAlbum = document.getElementById(&quot;sneakPeak&quot;);
  38. getAlbum.onclick=function(){
  39. newAlbum();
  40. }
  41. function loadDoc() {
  42. var xhttp = new XMLHttpRequest();
  43. xhttp.onreadystatechange = function(){
  44. if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200){
  45. document.getElementById(&quot;ExtraText&quot;).innerHTML = xhttp.responseText;
  46. }
  47. };
  48. xhttp.open(&quot;GET&quot;, &quot;TourInfo.txt&quot;, true);
  49. xhttp.send();
  50. }

The page I want the text file to appear on

  1. &lt;?php
  2. session_start();
  3. ?&gt;
  4. &lt;!doctype html&gt;
  5. &lt;html lang=&quot;en&quot;&gt;
  6. &lt;head&gt;
  7. &lt;meta charset=&quot;utf-8&quot;&gt;
  8. &lt;title&gt;CMP204 Unit Two Coursework Template&lt;/title&gt;
  9. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh&quot; crossorigin=&quot;anonymous&quot;&gt;
  10. &lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot;&gt;
  11. &lt;/head&gt;
  12. &lt;body&gt;
  13. &lt;h1&gt;User Profile&lt;/h1&gt;
  14. &lt;nav&gt;
  15. &lt;?php include_once &quot;includes/links.php&quot; ?&gt;
  16. &lt;/nav&gt;
  17. &lt;?php
  18. echo &quot;Welcome &quot; . $_SESSION[&quot;accountName&quot;] . &quot;.&lt;br&gt;&quot;;
  19. ?&gt;
  20. &lt;button id=&quot;accountInfo&quot; type=&quot;button&quot; class=&quot;btn btn-info btn-md&quot;&gt;Click here to learn about account privileges&lt;/button&gt; // this button should display the text from the server
  21. &lt;p id=&quot;perksDiv&quot;&gt; &lt;p&gt; // the text from the server should be showing in this paragraph
  22. &lt;div class=&quot;container-fluid&quot;&gt;
  23. &lt;div class=&quot;row&quot;&gt;
  24. &lt;div class=&quot;col-sm-4&quot;&gt;
  25. &lt;form action=&quot;addGigsAttended.php&quot; method=&quot;post&quot;&gt;
  26. add gig attended
  27. &lt;input type=&quot;text&quot; name=&quot;GigToAdd&quot;&gt;
  28. &lt;input type=&quot;submit&quot;&gt;
  29. &lt;/form&gt;
  30. &lt;/div&gt;
  31. &lt;div class=&quot;col-sm-4&quot;&gt;
  32. &lt;form action=&quot;editGigsAttended.php&quot; method=&quot;post&quot;&gt;
  33. edit gig you want to change
  34. &lt;input type=&quot;text&quot; name=&quot;GigToEdit&quot;&gt;
  35. &lt;br&gt;
  36. select gig to replace it with
  37. &lt;input type=&quot;text&quot; name=&quot;GigToReplace&quot;&gt;
  38. &lt;input type=&quot;submit&quot;&gt;
  39. &lt;/form&gt;
  40. &lt;/div&gt;
  41. &lt;div class=&quot;col-sm-4&quot;&gt;
  42. &lt;form action=&quot;deleteGigsAttended.php&quot; method=&quot;post&quot;&gt;
  43. delete gig attended
  44. &lt;input type=&quot;text&quot; name=&quot;GigToDelete&quot;&gt;
  45. &lt;input type=&quot;submit&quot;&gt;
  46. &lt;/form&gt;
  47. &lt;/div&gt;
  48. &lt;/div&gt;
  49. &lt;/div&gt;
  50. &lt;p&gt;Here are all the gigs you have attended&lt;/p&gt;
  51. &lt;/br&gt;
  52. &lt;?php
  53. $servername = &quot;lochnagar.abertay.ac.uk&quot;;
  54. $username = &quot;sql2100904&quot;;
  55. $password = &quot;ftAYZgzXz6au&quot;;
  56. $dbname = &quot;sql2100904&quot;;
  57. $conn = mysqli_connect($servername, $username, $password, $dbname);
  58. $accountName = $_SESSION[&quot;accountName&quot;];
  59. $getGigs = mysqli_prepare($conn, &quot;SELECT gigName FROM $accountName&quot;);
  60. $results = mysqli_stmt_execute($getGigs);
  61. mysqli_stmt_bind_result($getGigs, $GigLocation);
  62. while (mysqli_stmt_fetch($getGigs)) {
  63. echo $GigLocation . &quot; &quot;;
  64. }
  65. mysqli_stmt_close($getGigs);
  66. myslqi_close($conn);
  67. ?&gt;
  68. &lt;!--these scripts are necessary for Bootstrap and must be before the close body tag--&gt;
  69. &lt;script src=&quot;https://code.jquery.com/jquery-3.4.1.min.js&quot; integrity=&quot;sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  70. &lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js&quot; integrity=&quot;sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  71. &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  72. &lt;script src=&quot;javascript/script2.js&quot;&gt;&lt;/script&gt;
  73. &lt;/body&gt;
  74. &lt;/html&gt;

答案1

得分: 2

只使用一个$(document)函数,如果你使用太多的$(document)函数,那么响应中只会返回最后一个。

  1. $(document).ready(function(){
  2. // 在此处使用一个文档就绪函数并编写你的代码
  3. })
  4. 然后使用你想要运行的其他函数加载到window.load函数中
英文:

Use only 1 document function, if you will use too many $(document) functions than in response only last one will be returend.

  1. $(document).ready(function(){
  2. //use one doc ready and do your code in this
  3. })

And use your other function that you want to run will be loaded in window.load function

huangapple
  • 本文由 发表于 2023年1月9日 05:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定