Ajax -- $.get() 方法解析 JSON 数据
程序员文章站
2024-01-30 10:59:28
...
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
function RandomString(length) {
var str = '';
for ( ; str.length < length; str += Math.random().toString(36).substr(2) );
return str.substr(0, length);
}
$(function(){
$("#btn02").click(function(){
//$.get(URL,data,success(response,status,xhr),dataType);
$.get("AjaxJqueryGetServlet",{userName:'Jerry',passWord:'Jerry12345',date:RandomString(32)},function(data,status,xhr){
alert("后台返回数据: " + data.userName + ", " + data.passWord);
alert("状态: " + status);
alert("数据类型:" + xht);
},"json");
});
});
</script>
</head>
<body>
<center>
<button id="btn01">$.post() 方法解析 JSON 数据</button><br/>
<button id="btn02">$.get() 方法解析 JSON 数据</button>
</center>
</body>
</html>
package com.atguigu.ajax.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
@WebServlet("/AjaxJqueryGetServlet")
public class AjaxJqueryGetServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String> map = new HashMap<>();
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
map.put("userName", userName);
map.put("passWord", passWord);
Gson gson = new Gson();
String gsonMap = gson.toJson(map);
PrintWriter out = response.getWriter();
out.write(gsonMap);
}
}
推荐阅读
-
Ajax -- $.get() 方法解析 JSON 数据
-
(八) Node的POST和GET获取数据的方法
-
解析MSSQL跨数据库查询的实现方法_MySQL
-
解析PHP 使用curl提交json格式数据
-
解析curl提交GET,POST,Cookie的简单方法
-
ajax get请求得到了一个json格式的数据,在js中如何遍历出来
-
PHP XML数据解析代码,json,parser函数
-
iOS开发网络篇 一一 复杂的JSON解析-数据显示
-
jquery中get,post和ajax方法的使用小结_jquery
-
Android开发:JSON简介及最全面解析方法(Gson、AS自带org.json、Jackson解析)