zoukankan      html  css  js  c++  java
  • jquery 提交数据

    目录 jquery ajax的应用

    1 表单提交

    2 ajax的提交(ajax post get)

    普通的表单提交

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <%    
    String path = request.getContextPath();    
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";    
    pageContext.setAttribute("basePath",basePath);    
    %>    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'login.jsp' starting page</title>
    
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
      </head>
      
    <!--  <script src="js/jquery_1.83min.js"></script>-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
      <body>
        <div>
        <form id="form1" action="" >
                <input type="text" name="username"><br>
                <input type="text" name="password"><br>
                <input type="button" value="登录" id="but1">
        </form>
        </div>
        <script type="text/javascript">
        $('#but1').click(function(){
        $('#form1').attr({'action':"${pageScope.basePath}login",'method':"post"}).submit();
        });
        </script>
    </html>

    $.ajax

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <%    
    String path = request.getContextPath();    
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";    
    pageContext.setAttribute("basePath",basePath);    
    %>    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'login.jsp' starting page</title>
    
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
      </head>
      
    <!--  <script src="js/jquery_1.83min.js"></script>-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
      <body>
        <div>
        <form id="form1" action="" >
                <input type="text" name="username"><br>
                <input type="text" name="password"><br>
                <input type="button" value="登录" id="but1">
        </form>
        </div>
        <script type="text/javascript">
        $('#but1').click(function(){
            var user={username:"sunfan",password:'helloworld'};
            $.ajax({
                url:"login", //访问地址--action地址
                type:"post", //提交方式
                data:user,   //提交给服务器的数据
                success:function(reData){ //回调函数的处理方式
                alert(reData);
                }
            });
        });
        </script>
    </html>

    $.post

    $('#but1').click(function(){
            var user={username:"sunfan",password:'helloworld'};
               $.post( //提交方式
                  "login", //访问地址
                  user,   //提交给服务器的数据
                  function(reData){//回调函数的处理方式
                  alert(reData);
                  }
               );
        });
    

    $.get与post类似只是提交的方式不同

        $('#but1').click(function(){
            var user={username:"sunfan",password:'helloworld'};
               $.get(
                  "login",
                  user,
                  function(reData){
                  alert(reData);
                  }
               );
        });
  • 相关阅读:
    Unix Programming :文件IO
    Git 小记
    Effective C++ Placement new
    Effective C++ 避免数组多态
    系列文章:云原生Kubernetes日志落地方案
    阿里巴巴大数据产品最新特性介绍--机器学习PAI
    Apache Flink 1.9.0版本新功能介绍
    Flink Checkpoint 问题排查实用指南
    进击的 Java ,云原生时代的蜕变
    8 分钟入门 K8s | 详解容器基本概念
  • 原文地址:https://www.cnblogs.com/sunfan1988/p/3575670.html
Copyright © 2011-2022 走看看