$.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”

【1】JSP页面】

<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里面!!!

AJAX - $.get( url, [data], [success], [type] )_json


【2】后台接收思路

讲post的时候已经说明,request.getReader()只能获取请求体(form data)的数据,URL的数据(query string)获取不到!

① 使用​​request.getParameterMap​​;

该方法不分Form Data or Query String Parameters , 而是将数据以​​Map<String,String[]>​​返回。同名称的key对应的值,组成一个String[ ];

参考前篇post 的后台接收方法2:后台接收方法2

② 使用​​request.getQueryString​​;

import org.codehaus.jackson.map.ObjectMapper;

public class GetServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
//拿到$("#editform").serialize();
String jsonString =request.getQueryString() ;
System.out.println(jsonString);

//通过拿到的querystring转换为map
HashMap<String, Object> map = getMapFromQueryString(jsonString);

//将转换得到的map转换为json并返回
ObjectMapper objectMapper = new ObjectMapper();
String resultJson = objectMapper.writeValueAsString(map);
System.out.println("resultJson ..."+resultJson);

out.print(resultJson);
out.flush();
out.close();
}

public HashMap<String, Object> getMapFromQueryString(String queryString) {
//这个方法可能不适用你
HashMap map = new HashMap<String, String>();
String[] qStrings = queryString.split("&");
for (String string : qStrings) {
String[] qStrings2 = string.split("=");
map.put(qStrings2[0], qStrings2[1]);
}

return map;

}
}

result as follows :

getServlet init()....
username=yanshi02&password=123456
resultJson ...{"username":"yanshi02","password":"123456"}

此时的json为typeof data = object 可以直接使用data.key 获取属性,不用进行转换!!!


题外话

上面所传输的数据格式为​​username=yanshi02&password=123456​​。

如果传输的数据为JSON形式呢?

queryString = $('#editform').serializeArray();//JSON 数组

queryString = JSON.stringify(queryString);//string

//queryString = $('#editform').serializeObject();//JSON对象

请看下一篇JSON形式​