标签:void script http ram write har request parameter else
用ajax提交表单,提交按钮不能写成submit而应该写在button,否则是表单提交,而不是ajax效果
提交表单,form的 method, action都不用写了
一个小例子
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Test</title> <link rel="stylesheet" href=""> </head> <body> <form id="form1"> username: <input type="text" name="username" id="username" /> password: <input type="password" name="password" id="password" /> <input type="button" value="登录" id="send"/> </form> <script src="jquery.js"></script> <script> $("#send").click(function () { $.ajax({ type: "post", url: "../ajax/Test", //data: {username: $("#username").val(), password: $("#password").val()}, data: $("#form1").serialize(), success: function (data,textStatus) { if (data=="success") { alert("登录成功"); } else { alert("登录失败"); } } }); }); </script> </body> </html>
loginCheck.java
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Test extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); String username = req.getParameter("username"); String password = req.getParameter("password"); if (username.equals(password)) { out.print("success"); } else { out.print("fail"); } } }
标签:void script http ram write har request parameter else
原文地址:http://www.cnblogs.com/loveyunk/p/6036773.html