微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ajax jsp指定提交

文件结构:



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代码
  1. <strong><script>
  2. $(function(){
  3. $("input[type='button']").bind("click",function(){
  4. /**Ajax的请求*/
  5. $.ajax({
  6. //请求的路径
  7. url:"json.html",
  8. //是否异步
  9. async:true,0)">//请求的方法
  10. type:"get",0)">//请求成功时调用
  11. success:function(msg){
  12. alert(msg);
  13. },0)">//请求失败时调用
  14. error:function(msg){
  15. alert(msg);
  16. }
  17. });
  18. });
  19. });
  20. </script>
  21. </strong>

<!—body部分-->

二、Ajax请求jsp(传参数)

1get请求

<!—user.jsp-->

Java代码
  • <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  • <%
  • Stringname=request.getParameter("name");
  • if("kouxiaolin".equals(name)){
  • out.print("用户名正确");
  • }else{
  • out.println("用户名错误");
  • }
  • %>
  • 2post请求

    Js代码 //参数也可以在前面定义好,然后再后面调用
  • //varobj={name:"kouxiaolin",pass:"123"};$("input[type='button']").bind("click",function(){
  • //请求的路径
  • url:"user.jsp",0)">//请求方式
  • type:"post",0)">//所传参数多个参数用&连接:data:"name=kouxiaolin&pass=123"
  • data:"name=kouxiaolin",0)">//data:obj,
  • function(msg){
  • alert(msg);
  • }
  • });
  • });
  • });
  • </script>
  • </strong>
  • 三、Ajax请求解析json

    Js代码 //请求路径
  • url:"user.html",0)">//请求成功是调用
  • success:function(msg){
  • alert(msg.name);//返回kouxiaolin
  • },0)">//请求解析返回的类型是json类型
  • dataType:"json"
  • });
  • });
  • });
  • </script>
  • </strong>
  • 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

    相关推荐