Skip to content

Instantly share code, notes, and snippets.

@Hoff97
Created October 24, 2013 22:18
Show Gist options
  • Save Hoff97/7146111 to your computer and use it in GitHub Desktop.
Save Hoff97/7146111 to your computer and use it in GitHub Desktop.
Simple to use Ajax Object.
function AjaxRequest(_file, _readyFunction, _parameters) //A Ajax request wich sends parameter with POST
{
var file = _file;
var readyFunction = _readyFunction;
var parameters = _parameters;
this.send = function()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
readyFunction(xmlhttp);
}
else if(xmlhttp.status==404)
{
alert("Request failed. Page not found\nStopped at readyState: " + x
}
}
xmlhttp.open("POST",file,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var sendparams = "";
for(var a = 0; a<parameters.length; a++)
{
if(a>0)
{
sendparams += "&";
}
sendparams += parameters[a].name + "=" + parameters[a].value;
}
xmlhttp.send(sendparams);
}
}
function Parameter(_name, _value)
{
this.name = _name;
this.value = _value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment