웹프로그래밍

Global It Leader!!


jQuery


 
 

Ajax를 이용해 데이터를 서버로 보내는 몇 가지 방법

페이지 정보

작성자 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 1,891회 작성일 21-08-04 19:12

본문



1. 배열 전송하기

Client

Method : POST

Content-type : application/x-www-form-urlencoded; charset=UTF-8


    $.ajax({

        url: "/test",

        dataType: "json",

        contentType: "application/x-www-form-urlencoded; charset=UTF-8",

        type: "post",

        data: {ids : [1,2,3]},

        success: function (res) {

        },

        error: function (request, status, error) {

        }

    });

Server


    @PostMapping(value = "/test")

    @ResponseBody

    @ResponseStatus(HttpStatus.NO_CONTENT)

    public void test(

            @RequestParam(name = "ids[]", required = false) List<Integer> ids

    ) {

        logger.info("ids : {}", ids);

    }

2. 배열 전송하기(JSON)

Client

Method : POST

Content-type : application/json; charset=UTF-8


    $.ajax({

        url: "/test",

        dataType: "json",

        contentType: "application/json; charset=UTF-8",

        type: "post",

        data: JSON.stringify([1, 2, 3]),

        success: function (res) {

        },

        error: function (request, status, error) {

        }

    });

Server


    @PostMapping(value = "/test")

    @ResponseBody

    @ResponseStatus(HttpStatus.NO_CONTENT)

    public void test(

            @RequestBody(required = false) List<Integer> ids

    ) {

        logger.info("ids : {}", ids);

    }

3. 배열과 데이터 전송하기(JSON)

Client

Method : POST

Content-type : application/json; charset=UTF-8


    $.ajax({

        url: "/test",

        dataType: "json",

        contentType: "application/json; charset=UTF-8",

        type: "post",

        data: JSON.stringify({ids: [1, 2, 3], name: "kim"}),

        success: function (res) {

        },

        error: function (request, status, error) {

        }

    });

Server


Json 데이터를 바인딩할 객체


public class Test {


    private List<Integer> ids;

    private String name;

    

    //setter, getter 생략

    

}

    @PostMapping(value = "/test")

    @ResponseBody

    @ResponseStatus(HttpStatus.NO_CONTENT)

    public void test(

            @RequestBody(required = false) Test test

    ) {

        logger.info("test : {}", test);

    }

댓글목록

등록된 댓글이 없습니다.

전체 142
게시물 검색
jQuery 목록
번호 제목 글쓴이 조회 날짜
22 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2175 06-25
21 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1933 03-26
20 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1921 05-28
19 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1921 03-26
열람중 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1892 08-04
17 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1771 03-27
16 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1668 03-26
15 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1596 03-26
14 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1499 03-26
13 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1461 03-26
12 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1409 03-26
11 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1324 03-27
10 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1322 03-26
9 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1298 03-26
8 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1293 03-28
7 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1290 03-31
6 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1280 03-27
5 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1278 03-27
4 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1251 03-26
3 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 771 12-07