我正在嘗試使用 Ajax 通過獲取請求將資料從超鏈接傳遞到頁面
最初我可以在沒有 Ajax 的情況下直接做到這一點,如下所示
<a id="link" href="page.php?id=12&pid=12" >pass data</a>
我在 php 中找到了下面的內容
<?php $_GET['id'] ?>
<?php $_GET['pid']?>
現在我想使用 Ajax 執行此操作,因此不需要加載頁面
我正在嘗試以下
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
<!--insert.php calls the PHP file-->
url: "votes.php",
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
但我無法讓它發揮作用。我需要幫助正確實作使用 Ajax 發送資料到我的 php 檔案
uj5u.com熱心網友回復:
首先,<script>JavaScript 部分需要一個標簽。阿賈克斯URL可以讀取<a>標簽href的屬性$(this).attr('href')。同樣根據您的代碼,vote需要<div id="vote"></div>添加一個帶有id的 div 。
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
<div id="vote"></div>
<script>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
url: $(this).attr('href'),
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/388232.html
上一篇:如何從服務器上的URL或檔案而不是檔案輸入將檔案附加到FormData()
下一篇:.net核心mvc正在到達我發布的json控制器,但我無法讀取它。當我使用[FromBody]時,我也收到錯誤代碼415
