JSON APIs and Ajax: Post Data with the JavaScript XMLHttpRequest Method
In the previous examples, you received data from an external resource. You can also send data to an external resource, as long as that resource supports AJAX requests and you know the URL.
JavaScript’s XMLHttpRequest method is also used to post data to a server. Here’s an example:
req=new XMLHttpRequest();
req.open(“POST”,url,true);
req.setRequestHeader(‘Content-Type’,‘text/plain’);
req.onreadystatechange=function(){
if(req.readyState==4 && req.status==200){
document.getElementsByClassName(‘message’)[0].innerHTML=req.responseText;
}
};
req.send(userName);