반응형
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은 에 있습니다.json
post 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
반응형
'sourcecode' 카테고리의 다른 글
레일: 렌더링 Json 상태 문제 (0) | 2023.03.15 |
---|---|
상수 또는 읽기 전용 속성이므로 '상태'에 할당할 수 없습니다. (0) | 2023.03.15 |
REST: 한 번의 요청으로 여러 리소스 업데이트 - 표준입니까, 아니면 피해야 합니까? (0) | 2023.03.15 |
FTP 액세스를 제공하지 않고 WordPress 플러그인을 설치/업데이트할 수 있습니까? (0) | 2023.03.15 |
HttpResponseMessage 콘텐츠를 텍스트로 읽는 방법 (0) | 2023.03.15 |