Encapsulation of Ajax JSON / fromdata request in native JS

ajaxPost

function ajaxPost(url,json,cb){
	var xhr = new XMLHttpRequest();
	xhr.open('post',url);
	xhr.setRequestHeader("content-type","application/json");
	xhr.send(JSON.stringify(json));
	xhr.onload = function(){
		cb(JSON.parse(xhr.responseText));
	}
}

Posting pictures

function ajaxPic(formObj,url,cb){
	var formData = new FormData(formObj);
	console.log(formData);
	var xhr = new XMLHttpRequest();
	xhr.open('post',url);
	xhr.send(formData);
	xhr.onload = function(formData){
		cb(JSON.parse(xhr.responseText));
	}
}

Read More: