文件结构:
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",//请求失败时调用 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代码
- @H_502_55@<strong><script>
- @H_502_55@$(function(){
- @H_502_55@$("input[type='button']").bind("click",function(){
- @H_502_55@/**Ajax的请求*/
- @H_502_55@$.ajax({
- @H_502_55@//请求的路径
- @H_502_55@url:"json.html",
- @H_502_55@//是否异步
- @H_502_55@async:true,0)">//请求的方法
- @H_502_55@type:"get",0)">//请求成功时调用
- @H_502_55@success:function(msg){
- @H_502_55@alert(msg);
- @H_502_55@},0)">//请求失败时调用
- @H_502_55@error:function(msg){
- @H_502_55@alert(msg);
- @H_502_55@}
- @H_502_55@});
- @H_502_55@});
- @H_502_55@});
- @H_502_55@</script>
- @H_502_55@</strong>
<!—body部分-->
二、Ajax请求jsp(传参数)
1、get请求
Js代码
@H_502_55@<strong><scripttype="text/javascript">
@H_502_55@$(//请求的路径及所传的参数
@H_502_55@url:"user.jsp?name=kouxiaolin",85)">function(msg){
@H_502_55@alert(msg);
@H_502_55@}
@H_502_55@});
@H_502_55@});
@H_502_55@});
@H_502_55@</script>
@H_502_55@</strong>
<!—user.jsp-->
Java代码
@H_502_55@<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
@H_502_55@<%
@H_502_55@Stringname=request.getParameter("name");
@H_502_55@if("kouxiaolin".equals(name)){
@H_502_55@out.print("用户名正确");
@H_502_55@}else{
@H_502_55@out.println("用户名错误");
@H_502_55@}
@H_502_55@%>
2、post请求
Js代码
//参数也可以在前面定义好,然后再后面调用
@H_502_55@//varobj={name:"kouxiaolin",pass:"123"};$("input[type='button']").bind("click",function(){
@H_502_55@//请求的路径
@H_502_55@url:"user.jsp",0)">//请求方式
@H_502_55@type:"post",0)">//所传参数多个参数用&连接:data:"name=kouxiaolin&pass=123"
@H_502_55@data:"name=kouxiaolin",0)">//data:obj,
@H_502_55@function(msg){
@H_502_55@alert(msg);
@H_502_55@}
@H_502_55@});
@H_502_55@});
@H_502_55@});
@H_502_55@</script>
@H_502_55@</strong>
三、Ajax请求解析json