[Java] 網頁前端傳資料給後端,透過Ajax
筆記一下前端傳值給後端的寫法 前端.html <body> <script type="text/javascript"> $.ajax({ url:"./autocomplete", dataType: "json", type: "POST", async: false, timeout: 5000, data: { standardQ: "ABC" //傳給API的參數 }, success: function(data){ //成功的話,執行此區塊 alert("success"); }, }) </script> </body> 後端.java import java.io.IOException; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/AutoCompleteServlet") public class AutoCompleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AutoCompleteServlet() { super(); }