JavaScript请求后台数据的几种常用方式

0、本博客所用到的服务器端的代码

@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请求后台</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中的值传到后台,区别是后台请求后台之后,默认会打开新的浏览器窗口。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>JavaScript请求后台</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请求后台</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请求后台</title>
        <script type="text/javascript">
            function fun() {
               var form = document.createElement("form");
               //form.action="${pageContext.request.contextPath}/demoServlet?data=haha"; //这种方式不能将数据传递到后台
               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请求后台</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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁云亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值