$.get(url,data,success(response,status,xhr),dataType)
url:必需。规定将请求发送的哪个 URL。
data:可选。规定连同请求发送到服务器的数据。
success(response,status,xhr):可选。规定当请求成功时运行的函数。
额外的参数:
response - 包含来自请求的结果数据
status - 包含请求的状态
xhr - 包含 xmlHttPRequest 对象 dataType:可选。规定预计的服务器响应的数据类型。
默认地,jQuery 将智能判断。
可能的类型:”xml”“html”“text”“script”“json”“jsonp”
【jsp页面】
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'post.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"> <!-- <link rel="stylesheet" type="text/CSS" href="styles.css"> --> <script type="text/Javascript" src="./scripts/jquery-1.7.2.js"></script> <script type="text/javascript"> var url =""; var queryString = ""; $(function(){ $("input[type='button']").click(function(){ url = $("#editform").attr("action"); queryString = $('#editform').serialize(); $.get(url+"?querystring="+queryString,queryString,function(data){//querystring=username=yanshi02&password=123456&username=yanshi02&password=123456//resultJson ...{"username":"yanshi02","querystring":"username=yanshi02","password":"123456"} //$.get(url+"?querystring="+queryString,function(data){ //querystring=username=yanshi02&password=123456 //resultJson ...{"querystring":"username=yanshi02","password":"123456"} //$.get(url,queryString,function(data){ //username=yanshi02&password=123456 //resultJson ...{"username":"yanshi02","password":"123456"} alert(data); } ,"json"); }); }); </script> </head> <body> This is my JSP page. <br> <form id="editform" action="postServlet2" method="get"> <input type="text" name="username" value=""/> <input type="password" name="password" value=""/> </form> <input name="post" type="button" value="提交"/> </body></html>如下图标明:
get方式 数据都在Query String Parameters里面!!!
【后台接收思路】:
① 使用request.getParameterMap
;
该方法不分Form Data or Query String Parameters , 而是将数据以Map<String,String[]>
返回。同名称的key对应的值,组成一个String[ ];
参考前篇post 的后台接收方法2:后台接收方法2 http://blog.csdn.net/j080624/article/details/54946974
② 使用request.getQueryString
;
result as follows :
getServlet init()....username=yanshi02&password=123456resultJson ...{"username":"yanshi02","password":"123456"}题外话
上面所传输的数据格式为username=yanshi02&password=123456
。
如果传输的数据为JSON形式呢?
queryString = $('#editform').serializeArray();//objectqueryString = JSON.stringify(queryString);//string请看下一篇JSON形式【http://blog.csdn.net/J080624/article/details/54949773】
新闻热点
疑难解答