码迷,mamicode.com
首页 > Web开发 > 详细

Ajax知识点总结

时间:2016-11-10 14:38:53      阅读:135      评论:0      收藏:0      [点我收藏+]

标签: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");
        }
    }
}        

 

Ajax知识点总结

标签:void   script   http   ram   write   har   request   parameter   else   

原文地址:http://www.cnblogs.com/loveyunk/p/6036773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!