The basic code to use method GET to obtain the contents at a URL (or to ping the URL):
<script type="text/javascript">
function RequestMethodGET(URL)
{
var http = new XMLHttpRequest();
if(! http) { return false; }
http.onreadystatechange = function()
{
// When no response handling is desired, the next block may be removed in its entirety.
if(http.readyState == 4)
{
if(http.status == 200)
{
// Any actions to take when the server at the URL responds (response is in the http.responseText variable).
}
else { alert("Error: "+http.status+" "+http.statusText); }
}
}
http.open("GET",URL,true);
http.send('');
}
</script>
The basic code to use method POST for sending information to a URL and optionally retrieving its contents:
<script type="text/javascript">
function RequestMethodPOST()
{
var http = new XMLHttpRequest();
if(! http) { return false; }
var URL = "http://example.com/CHANGE-ME.php";
var params = new Array(); // UPDATE THESE
params.push( encodeURIComponent("a name") + "=" + encodeURIComponent("the name's value") );
params.push( encodeURIComponent("another name") + "=" + encodeURIComponent("another value") );
http.onreadystatechange = function()
{
// When no response handling is desired, the next block may be removed in its entirety.
if(http.readyState == 4)
{
if(http.status == 200)
{
// Any actions to take when the server at the URL responds (response is in the http.responseText variable).
}
else { alert("Error: "+http.status+" "+http.statusText); }
}
}
http.open("POST",URL,true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params.join("&"));
}
</script>