복붙노트

[JQUERY] JQuery Post JSON 객체를 서버에 게시합니다

JQUERY

JQuery Post JSON 객체를 서버에 게시합니다

해결법


  1. 1.JSON을 서버로 보내려면 먼저 JSON을 만들어야합니다.

    JSON을 서버로 보내려면 먼저 JSON을 만들어야합니다.

    function sendData() {
        $.ajax({
            url: '/helloworld',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                name:"Bob",
                ...
            }),
            dataType: 'json'
        });
    }
    

    이것은 JSON을 POST var로 보내라는 Ajax 요청을 구조화하는 방법입니다.

    function sendData() {
        $.ajax({
            url: '/helloworld',
            type: 'POST',
            data: { json: JSON.stringify({
                name:"Bob",
                ...
            })},
            dataType: 'json'
        });
    }
    

    JSON은 이제 JSON POST VAR에있을 것입니다.


  2. 2.FormData ()를 사용할 수도 있습니다. 그러나 contentType을 false로 설정해야합니다.

    FormData ()를 사용할 수도 있습니다. 그러나 contentType을 false로 설정해야합니다.

    var data = new FormData();
    data.append('name', 'Bob'); 
    
    function sendData() {
        $.ajax({
            url: '/helloworld',
            type: 'POST',
            contentType: false,
            data: data,
            dataType: 'json'
        });
    }
    
  3. from https://stackoverflow.com/questions/10110805/jquery-post-json-object-to-a-server by cc-by-sa and MIT license