JSP使用jQuery异步提交

来源:互联网 发布:酷酷跑软件 编辑:程序博客网 时间:2024/06/06 02:19

在很多时候,我们都需要做jQuery提交。
这里就直接上源码了。
需要下载jquery.min.js

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><script>    function login() {        var username = $("#username").val();        var password = $("#password").val();        var json = {            "username" : username,            "password" : password,        };        $.ajax({            url : "login.jsp",            data : json,            success : function(result) {                alert(result);            },            error : function() {                alert("提交失败");            }        });    }</script><script type="text/javascript" src="jquery.min.js"></script><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Login</title></head><body>    用户名:    <input type="text" name="username" id="username">    <br>密码:    <input type="password" name="password" id="password">    <br>    <input type="submit" value="登录" onclick="login()"></body></html>

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%    request.setCharacterEncoding("UTF-8");    response.setCharacterEncoding("UTF-8");    String username = request.getParameter("username");    String password = request.getParameter("password");    if (username.equals(password))        out.print("账号与密码相同");    else        out.print("账号与密码不同");%>

效果如下:
这里写图片描述
这里写图片描述

按确定是不会刷新当前页面的。

1 0