Ajax知识点总结
Posted loveyunk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ajax知识点总结相关的知识,希望对你有一定的参考价值。
用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"); } } }
以上是关于Ajax知识点总结的主要内容,如果未能解决你的问题,请参考以下文章