JavaScript請求后臺(tái)數(shù)據(jù)的常用方法匯總
0、所用到的服務(wù)器端的代碼
@WebServlet(urlPatterns = "/demoServlet") public class DemoServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = request.getParameter("data"); response.getWriter().write("{'data':'"+data+"'}"); System.out.println("doPost:"+data); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = request.getParameter("data"); response.getWriter().write("{'data':'"+data+"'}"); System.out.println("doGet:"+data); } }
1、window.location.href
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>JavaScript請求后臺(tái)</title> <script type="text/javascript"> function fun() { window.location.href="${pageContext.request.contextPath}/demoServlet?data=haha"; } </script> </head> <body> <button onclick="fun()">請求</button> </body> </html>
2、window.open()
把js中的值傳到后臺(tái),區(qū)別是后臺(tái)請求后臺(tái)之后,默認(rèn)會(huì)打開新的瀏覽器窗口。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>JavaScript請求后臺(tái)</title> <script type="text/javascript"> function fun() { // window.open("${pageContext.request.contextPath}/demoServlet?data=haha"); //打開新的窗口 window.open("${pageContext.request.contextPath}/demoServlet?data=haha","_self"); //在原窗口中撕開 } </script> </head> <body> <button onclick="fun()">請求</button> </body> </html>
3、.submit()方法提交表單
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>JavaScript請求后臺(tái)</title> <script type="text/javascript"> function fun() { var form = document.forms["form1"]; form.action="${pageContext.request.contextPath}/demoServlet"; form.method="GET"; form.submit(); } </script> </head> <body> <form name="form1"> <input type="text" name="data"> </form> <button onclick="fun()">請求</button> </body> </html>
4、.submit()方法提交表單
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>JavaScript請求后臺(tái)</title> <script type="text/javascript"> function fun() { var form = document.createElement("form"); //form.action="${pageContext.request.contextPath}/demoServlet?data=haha"; //這種方式不能將數(shù)據(jù)傳遞到后臺(tái) form.action="${pageContext.request.contextPath}/demoServlet"; var input = document.createElement("input"); input.name="data"; input.value= "haha"; form.appendChild(input); form.method="GET"; document.body.appendChild(form); form.submit(); } </script> </head> <body> <button onclick="fun()">請求</button> </body> </html>
5、自定義AJAX
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>JavaScript請求后臺(tái)</title> <script type="text/javascript"> function fun() { var xhr; if (window.XMLHttpRequest) { xhr = new window.XMLHttpRequest; } else { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } xhr.onreadystatechange = success; var url="${pageContext.request.contextPath}/demoServlet?data=haha"; xhr.open("POST", url, false); xhr.send(); function success() { if (xhr.readyState == 4 && xhr.status == 200) { //回傳成功 console.info(xhr.responseText); return true; } else { return false; } } } </script> </head> <body> <button onclick="fun()">請求</button> </body> </html>
6、使用JQuery
請參看博客:JQuery AJAX
到此這篇關(guān)于JavaScript請求后臺(tái)數(shù)據(jù)的幾種常用方式的文章就介紹到這了,更多相關(guān)js請求后臺(tái)數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實(shí)現(xiàn)時(shí)間格式化的方式匯總
這篇文章介紹了JS實(shí)現(xiàn)時(shí)間格式化的方式,有需要的朋友可以參考一下2013-10-10Typescript 函數(shù)重載的實(shí)現(xiàn)
本文主要介紹了Typescript 函數(shù)重載的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04調(diào)試Node.JS的輔助工具(NodeWatcher)
Node.JS調(diào)試貌似比較麻煩,每次改完了要重啟一下Node進(jìn)程.GOOGLE上有個(gè)NPM模塊,可以監(jiān)控JS文件的更改,然后自動(dòng)重啟Node.但我下載后在windows里運(yùn)行報(bào)錯(cuò)2012-01-01微信小程序自定義純凈模態(tài)框(彈出框)的實(shí)例代碼
這篇文章主要介紹了微信小程序自定義純凈模態(tài)框(彈出框)的實(shí)例代碼,代碼簡答易懂,非常不錯(cuò),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03