Ajax jsp指定提交
来源:互联网 发布:局域网控制软件破解版 编辑:程序博客网 时间:2024/05/29 06:37
文件结构:
ajax url 获取提交
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(function(){$("input[type='button']").bind("click",function(){/**Ajax的请求*/$.ajax({//请求的路径及所传的参数url:"user.jsp",data:{//发送给数据库的数据 username:$("#username").val(), content:$("#content").val() },//是否异步async:true,//请求的方法type:"get",//请求成功时调用success:function(msg){alert(msg);},//请求失败时调用error:function(msg){alert(msg);}});});});</script></head><body><body> <input type="text" id ="username" class="username" /><input type="text" id ="content" class="content" /> <input type="button" value="Ajax请求" /> </body> </body></html>
user.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><% String name = request.getParameter("username");String content = request.getParameter("content"); if("zxl".equals(name)){ out.print("用户名正确"+content); }else{ out.println("用户名错误"); }%>
ajax 指定url提交:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(function(){$("input[type='button']").bind("click",function(){/**Ajax的请求*/$.ajax({//请求的路径及所传的参数url:"user.jsp?username=zxl",//是否异步async:true,//请求的方法type:"get",//请求成功时调用success:function(msg){alert(msg);},//请求失败时调用error:function(msg){alert(msg);}});});});</script></head><body><body> <input type="text" id ="username" class="username" /><input type="text" id ="content" class="content" /> <input type="button" value="Ajax请求" /> </body> </body></html>
=================================================================================
一、简单的Ajax请求
Js代码
- <strong><script>
- $(function() {
- $("input[type='button']").bind("click", function() {
- /**Ajax的请求*/
- $.ajax( {
- //请求的路径
- url : "json.html",
- //是否异步
- async : true,
- //请求的方法
- type : "get",
- //请求成功时调用
- success : function(msg) {
- alert(msg);
- },
- //请求失败时调用
- error : function(msg) {
- alert(msg);
- }
- });
- });
- });
- </script>
- </strong>
<!—body部分-->
Java代码
- <body>
- <input type="button" value="Ajax请求" />
- </body>
二、Ajax请求jsp(传参数)
1、get请求
Js代码
- <strong><script type="text/javascript">
- $(function(){
- $("input[type='button']").bind("click",function(){
- /**Ajax的请求*/
- $.ajax({
- //请求的路径及所传的参数
- url:"user.jsp?name=kouxiaolin",
- //是否异步
- async:true,
- //请求的方法
- type:"get",
- //请求成功时调用
- success:function(msg){
- alert(msg);
- },
- //请求失败时调用
- error:function(msg){
- alert(msg);
- }
- });
- });
- });
- </script>
- </strong>
<!—user.jsp-->
Java代码
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String name = request.getParameter("name");
- if("kouxiaolin".equals(name)){
- out.print("用户名正确");
- }else{
- out.println("用户名错误");
- }
- %>
2、post请求
Js代码
- <strong><script>
- $(function() {
- //参数也可以在前面定义好,然后再后面调用
- // var obj={name:"kouxiaolin",pass:"123"}; $("input[type='button']").bind("click", function() {
- /**Ajax的请求*/
- $.ajax( {
- //请求的路径
- url : "user.jsp",
- //是否异步
- async : true,
- //请求方式
- type : "post",
- //所传参数多个参数用&连接:data:"name=kouxiaolin&pass=123"
- data:"name=kouxiaolin",
- //data:obj,
- //请求成功时调用
- success : function(msg) {
- alert(msg);
- },
- //请求失败时调用
- error : function(msg) {
- alert(msg);
- }
- });
- });
- });
- </script>
- </strong>
三、Ajax请求解析json
Js代码
- <strong><script>
- $(function() {
- $("input[type='button']").bind("click", function() {
- /**Ajax的请求*/
- $.ajax( {
- //请求路径
- url : "user.html",
- //是否异步
- async : true,
- //请求的方法
- type : "get",
- //请求成功是调用
- success : function(msg) {
- alert(msg.name);//返回kouxiaolin
- },
- //请求失败时调用
- error : function(msg) {
- alert(msg);
- },
- //请求解析返回的类型是json类型
- dataType:"json"
- });
- });
- });
- </script>
- </strong>
- Ajax jsp指定提交
- jsp页面ajax提交方式
- jsp中,ajax提交表单用法
- jsp不通过form和Ajax提交
- 【Jsp】Ajax提交数据不刷新页面
- Jsp页面简单的Ajax提交
- ajax提交指定数字键名数组的方法
- JSP中,AJAX使用POST方式提交中文乱码问题解决
- JSP中,AJAX使用POST方式提交中文乱码问题解决
- jquery ajax提交表单从action传值到jsp
- jsp采用ajax提交list到struts2传值
- jsp用Ajax提交表单并显示结果(一)
- jquery ajax提交表单从action传值到jsp实现小结
- springmvc 使用jsp生成pdf非ajax提交
- bootstrap Validator 模态框、jsp、表单验证 Ajax提交
- JSP表单提交和ajax提交表单数据是不同的,冲突的,不能同时进行
- ajax 提交
- Ajax提交
- MFC 多文档程序 手动创建新文档的方法
- D-Link DWA-160 wifi抓包
- Can't create thread to send RTP packets (SIPP 并发错误)
- ORACLE PL/SQL编程--异常错误处理
- ARM与X86不具备可比性 性能差距可大上千倍
- Ajax jsp指定提交
- 修改chrome的背景色
- 承接java项目
- 山之魂
- HDU3113(工科数学分析之分解)
- robotium源码分析 ClickView
- 2013年就要结束了,留下这一年所有的感动
- OpenGL ES2.0 的三种变量类型(uniform,attribute和varying)
- hadoop2.2编译