본문 바로가기
자바스크립트/Javascript

XMLHttpRequest를 통해 canvas의 그린 이미지를 서버에 던지기

by 디찌s 2021. 2. 26.
728x90
반응형
	//canvas에 그려진 이미지를 base64로 변환
    var dataurl = this.canvas.toDataURL("image/png");
	
    var xhr = new XMLHttpRequest();
	var data = {
		searchimage: dataurl
	};
	xhr.crossOrigin = "Anonymous";
	xhr.onload = function() {
	  if (xhr.status === 200 || xhr.status === 201) {
	    console.log(xhr.responseText);
	  } else {
	    console.error(xhr.responseText);
	  }
	};
	xhr.open('POST', '서버IP');
	xhr.setRequestHeader('Content-Type', 'application/json'); // 컨텐츠타입을 json으로
	xhr.send(JSON.stringify(data)); // 데이터를 stringify해서 보냄

 

728x90
반응형

댓글