快捷搜索: 王者荣耀 脱发

前端向后端发送请求的方式

1、 link标签的href属性

2、 script标签的src属性

3、 img标签的src属性

4、 ajax发送请求

5、 表单提交发送请求

6、 a标签的href发送请求

7、 iframe的src属性发送请求

1.form表单

前端代码:

<!--post代表提交方式,action代表提交的地址-->
<form method="post" action="userServlet" >
        姓名:<input type="text" name="userName" />
        年龄:<input type="text" name="age" />
        <!--注意提交按钮在form表单中-->
        <input type="submit" value="提交" />
</form>

后端代码:

2.AJAX提交

js总也有ajax提交,但是非常复杂,jQuery中的ajax提交就很简洁了,格式如下:

$.ajax({
        url:dataListServlet?department=+department+&gender=+gender,
        type:GET,
        contentType : false,
        processData : false,
        cache : false,
        success : function(data) {
            if(data){
                
            }else{
                alert("查询失败");
            }
        }
});

这里只是简单格式介绍,ajax提交详解:点击这里

冷知识:url中携参,提交方式用POST也可以提交,并且后台获取到的数据是地址栏中的参数,地址栏中的参数会覆盖data中的数据。

3.<a/>标签href属性提交,同样也可以拼接字符串,提交方式默认是GET提交

<a href="DeleteUserServlet?id=3&gender=男"></a>
  request.setAttribute("id","3");
  request.setAttribute("gender","男");
  request.getRequestDispatcher("updateUser.jsp").forward(request,response);

5.通过刷新本页的方式访问

 
window.location.href="DeleteUserServlet?deleteId="+deleteId;
经验分享 程序员 微信小程序 职场和发展