웹프로그래밍

Global It Leader!!


jQuery


 
 

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

페이지 정보

작성자 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 1,880회 작성일 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 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 430 07-06
141 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 654 01-05
140 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 754 12-07
139 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1276 03-31
138 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1278 03-28
137 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1265 03-27
136 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1744 03-27
135 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1261 03-27
134 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1306 03-27
133 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1579 03-26
132 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1284 03-26
131 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1446 03-26
130 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2766 03-26
129 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1921 03-26
128 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1482 03-26
127 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1653 03-26
126 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1906 03-26
125 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1308 03-26
124 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1233 03-26
123 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1394 03-26