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

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

jquery ajax() function not working or even being acknowledged

问题

我目前正在开发一个项目使用了jquery的ajax函数我只想让ajax函数在单击某个按钮时从服务器上显示一个文件中的一些文本但是当我点击按钮时它什么也没发生我尝试添加一个错误消息但似乎也不起作用我确保使用了支持ajax函数的jquery版本甚至复制粘贴了我知道有效的代码但似乎一切都不起作用

我的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> <!-- 这个按钮应该显示来自服务器的文本 -->
<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所必需的,必须放在关闭body标签之前-->
    <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>
英文:

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

window.onload=loadDoc(); 
$(document).ready(function(){   // the function that should be getting called 
$(&quot;#accountInfo&quot;).click(function(){ 
$.ajax({url: &quot;accountPerks.txt&quot;,  
error: function(xhr, status, error) {
var err = eval(&quot;(&quot; + xhr.responseText + &quot;)&quot;);
alert(err.Message); 
},
success: function(result){ 
$(&quot;#perksDiv&quot;).text(result);
}});
});
});  
//everything below here are functions for separate pages  
$(document).ready(function(){ 
$(&quot;#member1Info&quot;).click(function(){ 
$(&quot;#BandMember1&quot;).css(&quot;visibility&quot;, &quot;visible&quot;);
});
}); 
$(document).ready(function(){ 
$(&quot;#member2Info&quot;).click(function(){ 
$(&quot;#BandMember2&quot;).css(&quot;visibility&quot;, &quot;visible&quot;);
});
}); 
$(document).ready(function(){ 
$(&quot;#member3Info&quot;).click(function(){ 
$(&quot;#BandMember3&quot;).css(&quot;visibility&quot;, &quot;visible&quot;);
});
});
function newAlbum(){  
var node = document.createElement(&quot;li&quot;); 
var textNode = document.createTextNode(&quot;aaaaallllpxas(2023)&quot;); 
node.appendChild(textNode); 
document.getElementById(&quot;albumList&quot;).appendChild(node); 
$(&quot;#sneakPeak&quot;).css(&quot;visibility&quot;, &quot;hidden&quot;);
}
var getAlbum = document.getElementById(&quot;sneakPeak&quot;);  
getAlbum.onclick=function(){ 
newAlbum();
}
function loadDoc() { 
var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function(){ 
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200){ 
document.getElementById(&quot;ExtraText&quot;).innerHTML = xhttp.responseText;
}
}; 
xhttp.open(&quot;GET&quot;, &quot;TourInfo.txt&quot;, true); 
xhttp.send();
} 

The page I want the text file to appear on

&lt;?php 
session_start(); 
?&gt;
&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;CMP204 Unit Two Coursework Template&lt;/title&gt;
&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;
&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt; 
&lt;h1&gt;User Profile&lt;/h1&gt;
&lt;nav&gt;
&lt;?php include_once &quot;includes/links.php&quot; ?&gt;
&lt;/nav&gt;
&lt;?php 
echo &quot;Welcome &quot; . $_SESSION[&quot;accountName&quot;] . &quot;.&lt;br&gt;&quot;; 
?&gt; 
&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 
&lt;p id=&quot;perksDiv&quot;&gt; &lt;p&gt;    // the text from the server should be showing in this paragraph   
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;div class=&quot;row&quot;&gt;  
&lt;div class=&quot;col-sm-4&quot;&gt; 
&lt;form action=&quot;addGigsAttended.php&quot; method=&quot;post&quot;&gt; 
add gig attended 
&lt;input type=&quot;text&quot; name=&quot;GigToAdd&quot;&gt; 
&lt;input type=&quot;submit&quot;&gt; 
&lt;/form&gt; 
&lt;/div&gt;  
&lt;div class=&quot;col-sm-4&quot;&gt; 
&lt;form action=&quot;editGigsAttended.php&quot; method=&quot;post&quot;&gt; 
edit gig you want to change
&lt;input type=&quot;text&quot; name=&quot;GigToEdit&quot;&gt;  
&lt;br&gt; 
select gig to replace it with
&lt;input type=&quot;text&quot; name=&quot;GigToReplace&quot;&gt;
&lt;input type=&quot;submit&quot;&gt; 
&lt;/form&gt; 
&lt;/div&gt;  
&lt;div class=&quot;col-sm-4&quot;&gt; 
&lt;form action=&quot;deleteGigsAttended.php&quot; method=&quot;post&quot;&gt; 
delete gig attended 
&lt;input type=&quot;text&quot; name=&quot;GigToDelete&quot;&gt; 
&lt;input type=&quot;submit&quot;&gt; 
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;   
&lt;/div&gt; 
&lt;p&gt;Here are all the gigs you have attended&lt;/p&gt; 
&lt;/br&gt; 
&lt;?php 
$servername = &quot;lochnagar.abertay.ac.uk&quot;;
$username = &quot;sql2100904&quot;;
$password = &quot;ftAYZgzXz6au&quot;;
$dbname = &quot;sql2100904&quot;; 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
$accountName = $_SESSION[&quot;accountName&quot;];
$getGigs = mysqli_prepare($conn, &quot;SELECT gigName FROM $accountName&quot;); 
$results = mysqli_stmt_execute($getGigs); 
mysqli_stmt_bind_result($getGigs, $GigLocation); 
while (mysqli_stmt_fetch($getGigs)) {
echo $GigLocation . &quot; &quot;;
}
mysqli_stmt_close($getGigs); 
myslqi_close($conn);
?&gt;
&lt;!--these scripts are necessary for Bootstrap and must be before the close body tag--&gt;
&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;
&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;
&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;
&lt;script src=&quot;javascript/script2.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 2

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

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

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

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

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:

确定