웹프로그래밍

Global It Leader!!


jQuery


 
 

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

페이지 정보

작성자 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 1,881회 작성일 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 목록
번호 제목 글쓴이 조회 날짜
열람중 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1882 08-04
121 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2164 06-25
120 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 1906 05-28
119 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3090 03-06
118 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2900 03-02
117 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2673 11-06
116 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3265 11-03
115 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2666 10-28
114 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2768 10-06
113 no_profile 운영자 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2623 09-11
112 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2825 01-20
111 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2742 08-18
110 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2754 01-30
109 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3052 09-23
108 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 2949 02-04
107 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3014 02-04
106 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3196 09-05
105 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3270 02-23
104 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3243 12-27
103 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3325 12-21