闲话
最近要完成学校的java大作业,所以也要用JSON传值来具体实现,之前一直知道大概的过程但自己从来没有去实现过一直觉得挺简单,试一下才知道,原来自己还有很多不会呢,在网上看了看基本上都是利用现有的框架来进行传值,所以一直没找到最合适的,不过最后还是在google上找到了所以也记录一下。
准备工作:
*IDE :Eclipse Neon
client.jsp
<%@ page language="java" contentType="text/html; charset=BIG5"
pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<script src="jquery-3.1.1.min.js" type="text/javascript"></script>
</head>
<body>
<input type="button" id="btnSumit" name="btnSubmit" value="submit" />
</body>
<script type="text/javascript">
$("#btnSumit").click(function(){
alert("submit!");
$.ajax({
type: "POST",
url: "server.jsp",
data: {type: "i", txtUserId:"2", txtUserName: "3"},
//dataType: "text",
//dataType: "html",
dataType: "json",
cache: false,
success: function(response){
alert(response.exeStatus);
alert(response.msg);
},
});
});
</script>
</html>
server.jsp
<%@page import="org.json.JSONObject"%>
<%
JSONObject json = new JSONObject();
json.put("exeStatus", "Y");
json.put("msg", "OKOK");
out.println(json);
%>
ps:自己要去下载出来JSON的jar包,百度一下会有很多。
LAST:
最近也快期末了,做完手头的事情好好的复习,寒假跟着原先的计划走 :)