bestsource

JSON을 사용하여 XmlHttpRequest POST 작성

bestsource 2023. 3. 10. 22:38
반응형

JSON을 사용하여 XmlHttpRequest POST 작성

바닐라 JS를 사용하여 JSON 데이터를 전송하는 AJAX POST 요청을 작성하려면 어떻게 해야 합니까?

content-type은 url 형식으로 인코딩되어 있어 네스트된 JSON을 지원하지 않습니다.

플레인 구 JS에서 네스트된 JSON을 사용하여 POST 요청을 할 수 있는 방법이 있습니까?SO에서 제공되는 다양한 직렬화 방법을 시도했지만 모두 JSON을 하나의 형식으로 만듭니다.

여기 JSON이 있습니다.

{
   email: "hello@user.com",
   response: {
       name: "Tester"
   }
}

JSON을 올바르게 사용하면 문제 없이 중첩된 개체를 가질 수 있습니다.

var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
var theUrl = "/json-handler";
xmlhttp.open("POST", theUrl);
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify({ "email": "hello@user.com", "response": { "name": "Tester" } }));

언급URL : https://stackoverflow.com/questions/39519246/make-xmlhttprequest-post-using-json

반응형