Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- CSS
- RDS
- Cookie
- 비트코인
- jQuery
- express
- 웹소켓
- 알고리즘
- 웹게임
- EC2
- websocket
- AWS
- 배포
- tiles.xml
- docker
- 암호화
- 도커
- phaser
- autowired
- JavaScript
- JSP
- Spring
- 블록체인
- SQL
- Ajax
- Servlet
- PL/SQL
- HTML
- node.js
- model1
Archives
- Today
- Total
記錄
Javascript) 클라이언트에서 보낸 배열을 서버에서 받기 본문
Ex 1)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#sdata").click(function() {
var string = ["abc","def"];
var vParam = "test";
$.ajax({
url: "DataArray.jsp",
type: "GET",
data: {"string":string}, //jsp?string=abc&string=def&string=aaa
success: function(data) {
var as = eval(data);
alert("data:"+as[0]+"/"+as[1]);
},
error: function(msg, error) {
alert(error);
}
});
});// end .click()
}); //end jQuery ready
</script>
</head>
<body>
<input type="button" value="서버 전송" id="sdata">
</body>
</html>
//String[] aStr = request.getParameterValues("string"); 일반적인 동기형태 (checkbox)
String[] aStr = request.getParameterValues("string[]");
Ex 2)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#sdata").click(function(){
var jsondata = {"name":"kglim","age":100};
var stringJson = JSON.stringify(jsondata);
alert(stringJson);
$.ajax({
url: "DataArray2.jsp",
type: "GET",
data: {"json":stringJson},
//data:{"name":"kglim","age":100}; ?name=kglim&age=100
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
});
});
/* $.ajax({
url: "DataArray2.jsp",
type: "GET",
data: {"jsonArrayData":[{"idx":1,"date":{"title":"책","id":"abcd"}}]},
//data: {"jsonArrayData":"aaaaaaaaaaaaaaaa"},
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
});
}); */
</script>
</head>
<body>
<input type="button" value="서버 전송" id="sdata">
</body>
</html>
String data = request.getParameter("json");
//{"name":"kglim","age":100}
JSONParser json = new JSONParser();
JSONObject jsonobj = (JSONObject)json.parse(data);
//System.out.println(jsonobj);
System.out.println(jsonobj.get("name"));
System.out.println(jsonobj.get("age"));
Ex 3)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#sdata").click(function(){
var jsondata = {
"books": [
{
"genre": "소설",
"price": "100",
"name": "사람은 무엇으로 사는가?",
"writer": "톨스토이",
"publisher": "톨스토이 출판사"
},
{
"genre": "소설",
"price": "300",
"name": "홍길동전",
"writer": "허균",
"publisher": "허균 출판사"
},
{
"genre": "소설",
"price": "900",
"name": "레미제라블",
"writer": "빅토르 위고",
"publisher": "빅토르 위고 출판사"
}
],
"persons": [
{
"nickname": "남궁민수",
"age": "25",
"name": "송강호",
"gender": "남자"
},
{
"nickname": "예니콜",
"age": "21",
"name": "전지현",
"gender": "여자"
}
]
}
var stringJson = JSON.stringify(jsondata);
alert(stringJson);
$.ajax({
url: "DataArray3.jsp",
type: "GET",
data: {"json":stringJson},
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
});
});
/* $.ajax({
url: "DataArray2.jsp",
type: "GET",
data: {"jsonArrayData":[{"idx":1,"date":{"title":"책","id":"abcd"}}]},
//data: {"jsonArrayData":"aaaaaaaaaaaaaaaa"},
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
});
}); */
</script>
</head>
<body>
<input type="button" value="서버 전송" id="sdata">
</body>
</html>
String data = request.getParameter("json");
System.out.println(data);
JSONParser json = new JSONParser();
JSONObject jsonobj = (JSONObject)json.parse(data);
//System.out.println(jsonobj);
JSONArray bookInfoArray = (JSONArray)jsonobj.get("books");
for(int i=0; i<bookInfoArray.size(); i++){
//배열 안에 있는것도 JSON형식 이기 때문에 JSON Object 로 추출
JSONObject bookObject = (JSONObject) bookInfoArray.get(i);
System.out.println("bookInfo: name==>"+bookObject.get("name"));
System.out.println("bookInfo: writer==>"+bookObject.get("writer"));
System.out.println("bookInfo: price==>"+bookObject.get("price"));
System.out.println("bookInfo: genre==>"+bookObject.get("genre"));
System.out.println("bookInfo: publisher==>"+bookObject.get("publisher"));
}
'IT_Fundamental > etc' 카테고리의 다른 글
개발자 로드맵 (0) | 2019.09.07 |
---|---|
script에서 request에 parameter 설정 (0) | 2018.04.13 |
Model1 vs Model2 (간단히) (0) | 2018.03.30 |
Setting (0) | 2018.03.27 |
UML(Class Diagram) (0) | 2018.02.12 |
Comments