2025/05 3

자바스크립트 fetch()함수_ 파일업로드

자바스크립트 fetch()함수를 이용하여 파일 업로드let formElement = document.getElementById("form_ch1");let formData = new FormData(formElement);fetch("modal_img.php", { method: "POST", body: formData, headers: { // 'Content-Type': 'multipart/form-data' 👈 FormData 사용 시 명시하지 않음! (자동 생성됨) "Accept": "application/json" // 서버가 JSON 응답을 반환하도록 요청 }}).then(response => response.json()).then(data =>..

자바스크립트 fetch()함수_ $_POST

1. 자바스크립트 fetch()함수를 이용하여 전달하려는 데이터를 PHP 로 전달하는 방법.fetch("modal_img.php", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ title: "Test", body: "I am testing!", userId: 1 })}).then(response => response.text()) // JSON 대신 일반 텍스트 응답.then(data => console.log("서버 응답:", data)).catch(error => co..

카테고리 없음 2025.05.09

자바스크립트 fetch()함수_ json

1. 자바스크립트 fetch()함수를 이용하여 전달하려는 데이터를 josn 전환하여 PHP 로 전달하는 방법.2. PHP에서 전달 받은 데이터를 json형식으로 변경fetch("modal_img.php", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ // param title: "Test", body: "I am testing!", userId: 1, }),}).then((response) => console.log(response));PHP Json 받기 "success", "title" => $title, "body" => $body, ..