웹프로그래밍

Global It Leader!!


jQuery


 
 

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

페이지 정보

작성자 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 1,869회 작성일 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 목록
번호 제목 글쓴이 조회 날짜
142 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 421 07-06
141 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 645 01-05
140 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 742 12-07
139 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1264 03-31
138 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1268 03-28
137 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1255 03-27
136 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1727 03-27
135 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1250 03-27
134 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1292 03-27
133 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1562 03-26
132 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1272 03-26
131 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1429 03-26
130 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2739 03-26
129 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1907 03-26
128 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1468 03-26
127 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1641 03-26
126 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1890 03-26
125 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1295 03-26
124 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1220 03-26
123 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1381 03-26