본문 바로가기
Development (국비 복습 )/JavaScript

[Ajax] open() send()로 서버에 데이터 전송

by Puddingforever 2023. 2. 11.

ajax 기술을 사용할 때는, 웹페이지 안에서 바로 서버로 데이터를 전달할 수 있기 하기 위해 XMLHttpRequest()함수를 사용한다.

이때, 서버로 데이터를 전달하는 함수들이 있는데 , 대표적으로 open()과 send()를 들 수 있다.


open(method,url) method : 요청의 유형 (GET 또는 POST)
url : 서버의 위치
send() send: 서버로 데이터 요청 (GET)
function loadDoc(){

	const xhttp = new XMLHttpRequest();
 	xhttp.onload = function(){
    
    	document.getElementById("demo").innerHTML = 
        this.responseText;
    }
    
    xhttp.open("GET","ajax_info.txt");
    xhttp.send();


}

this.responseText : loadDoc()함수로 받은 데이터 결과값을 문자열로 반환 

 

 

 

이 코드 5번 안보고 만들기  ~~ 

https://www.w3schools.com/js/tryit.asp?filename=tryjs_ajax_first 

 

W3Schools online HTML editor

The W3Schools online code editor allows you to edit code and view the result in your browser

www.w3schools.com

 

 

 

댓글