[英]Simple AJAX button click (server is local) doesn't work
我對 AJAX 非常陌生,我嘗試了最簡單的方法之一(我認為),加載 HTML 網頁,單擊按鈕並顯示警告框。 由於某種原因,我嘗試過的所有示例(使用 GET、POST、不同的語法……)都不起作用。 我的網絡服務器是本地的,我使用 node js、express 和 ejs。 在互聯網上似乎有很多點擊這個按鈕的例子,但是本地服務器沒有任何效果。 我測試的瀏覽器是谷歌瀏覽器。
<script type="text/javascript">
$(document).ready(function(){
$('btn').on('click', function(){
$.ajax({
'url': '/gebruikerpagina',
'type': 'POST',
succes: function(st){
if(st === "succes"){
alert('Clicked');
}
}
})
});
});
</script>
<button type="button" id="btn">Change Content</button>
這是在我的 HTML 文件中(在頭部也導入了 jquery),接下來的代碼在我的 js 中:
app.post('/gebruikerpagina', function(req,res){
res.render('../gebruikerpagina')
});
考慮以下代碼。
$(function() { $('#btn').click(function() { $.ajax({ url: "./gebruikerpagina", type: "POST", succes: function(st) { console.log(st); if (st === "success") { console.log("Success"); } }, error: function(x, e, t) { console.log(e, t); } }) }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" id="btn">Change Content</button>
這種性質的代碼將為控制台提供更多反饋,並且更容易診斷。 此代碼還使用正確的選擇器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.