jQuery - Ajax 경유로 JSON을 PUT하는 방법
jQuery를 사용하여 Ajax를 통해 JSON 형식의 데이터를 서버에 저장하려고 합니다.코드는 다음과 같습니다.
$.ajax({
type: "PUT",
url: myURL,
contentType: "application/json",
data: {"data": "mydata"}
});
그러나 서버측에서는,data=mydata문자열(예상된 JSON 대신)을 지정합니다.불벌레도 마찬가지야
어디에 오류가 있습니까?
데이터가 문자열이어야 할 것 같아요.오브젝트는 쿼리 문자열로 변환됩니다.이 문자열은 다음과 같습니다.
를 사용할 수 있습니다.JSON.stringify(obj)메서드를 사용하여 개체를 문자열로 변환합니다.JSON 객체의 코드는 https://github.com/douglascrockford/JSON-js/blob/master/json2.js 에서 구할 수 있습니다.
또는 오브젝트를 리터럴 문자열로 작성하기 위해 사용하는 코드를 전달해 주세요.단, 이것은 예에 불과하기 때문에 이미 작성한 오브젝트를 부호화할 필요가 있습니다.
어플리케이션 내에서 항상 JSON을 전송해야 하는 경우 init 중 어딘가에서 이 작업을 수행한 후 default를 사용할 수 있습니다.$.ajax콜은 예시와 같이 항상 Ajax 기본 쿼리 문자열이 아닌 JSON 문자열로 시리얼화됩니다.
여기서는 위에서 설명한 JSON 개체를 사용합니다.
$.ajaxSetup({
contentType : 'application/json',
processData : false
});
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
if (options.data){
options.data=JSON.stringify(options.data);
}
});
//url: this is a reference to the XML, where you need to define the mapping.
//<entry key="/getEmpDetails/transEfileGenerate.app">
//<bean class="com.adp.ems.framework.spring.MappingItem" p:delegate-ref="efilePageDelegate"
//p:action="passJSONObjectAndGetWebServiceOutput" />
//str1 is the input JSON that you need to pass... Ajax will automatically take care to get the response.
//</entry>
var kw = {
url : "getEmpDetails/transEfileGenerate.app",
timeout : 30000,
handleAs : "json",
sync: false,
putData : str1,
headers: { "Content-Type": "application/json"},
load : function(result) {
},
언급URL : https://stackoverflow.com/questions/1749272/jquery-how-to-put-json-via-ajax
'source' 카테고리의 다른 글
| 최적의 사용자 역할 권한 데이터베이스 설계 프랙티스 (0) | 2023.02.16 |
|---|---|
| 중력 형태 오류 (0) | 2023.02.16 |
| AJAX를 사용하여 교차 도메인 끝점 로드 (0) | 2023.02.16 |
| "각도가 정의되지 않음"의 원인은 무엇입니까? (0) | 2023.02.12 |
| WooCommerce 모든 제품을 카트에서 삭제하고 현재 제품을 카트에 추가합니다. (0) | 2023.02.12 |