sourcecode

JQuery 서버에 JSON 개체를 게시합니다.

copyscript 2023. 3. 15. 19:52
반응형

JQuery 서버에 JSON 개체를 게시합니다.

jersey에 게시해야 하는 json을 만듭니다.REST 웹 서비스를 가진 grezly가 실행하는 서버가 출력해야 하는 수신 json 객체를 가져옵니다.시도하고 있지만 어떻게 하면 제대로 구현할 수 있을지 잘 모르겠습니다.

import java.io.IOException;
import java.io.InputStream;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.io.IOUtils;

import javax.ws.rs.*;

    @Path("/helloworld")
    public class GetData {
        @GET
        @Consumes("application/json")
        public String getResource() {

            JSONObject obj = new JSONObject();
            String result = obj.getString("name");

            return result;      
        }                   

    } 

다운로드 중에 이 메서드를 실행하는 html 파일이 있습니다.

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


                },
                dataType: 'json'
            });
            alert("json posted!");
        };

서버에 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은 에 있습니다.jsonpost var.

사용할 수도 있습니다.FormData()하지만, 당신은 세팅해야 합니다.contentType~하듯이false:

var data = new FormData();
data.append('name', 'Bob'); 

function sendData() {
    $.ajax({
        url: '/helloworld',
        type: 'POST',
        contentType: false,
        data: data,
        dataType: 'json'
    });
}

언급URL : https://stackoverflow.com/questions/10110805/jquery-post-json-object-to-a-server

반응형