簡體   English   中英

使用Ajax將表單數據發送到Node.js而不重新加載頁面

[英]Sending form data to nodejs without reloading page using Ajax

這是我的代碼,用於使用Ajax將表單數據發送到nodejs。 我想實現此目的而無需重新加載頁面。 我試圖在IE,Firefox和Chrome中運行它。 沒有看到輸出,因此我想知道我要去哪里。 我是Ajax的新手,請幫助我!

 <html> <meta charset="UTF-8"> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script> $('#newform').on('submit', function (event) { //alert('Hi'); event.preventDefault(); var data = { username: $('#username').val(), }; $.ajax({ url: 'http://127.0.0.1:8081/upload', data: data, method: 'POST' }).then(function (response) { $('body').append(response); }).catch(function (err) { console.error(err); }); }); </script> </head> <body> <form action="/upload" method="post" id="newform"> <input type="text" name="username" id="username"></input> <input type="button" value="submit" id="s1"></input> </form> </body> </html> 

我看到您包括了1.5版的jquery(現在是1.11 ..)。 很老了 但是運行您的代碼時,我看到以下錯誤: TypeError: $(...).on is not a function

如果我們轉到jQuery的.on方法文檔( http://api.jquery.com/on/ ),則可以看到它已包含在1.7版中,這意味着您所包含的版本中不存在它。

我的建議是將庫更新為1.11.3(最新) http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js

該代碼對我來說還不錯。 嗯,當您運行腳本時,應該在chrome的檢查器控制台中檢查錯誤,以便您可以查看出了什么問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM