Hello everyone,

I wrote an Ajax API, and I thought that it may be of some use to you all. I will post the code and explain it here.

/*
<!Tut

note: do not use "var" to declare x or req.

function makeReq()
{
target="http://yourserver.com/login.php";
data="yourdata=this&itsplace=thisstring";
req = new Request("post", target, true, data); /* method, address, async or not (true / false), optional: data. (it will auto detect)*/
req.rscAction = function() /* rsc = readystate call; when the readystate changes (req.request.onreadystatechange), the function that is executed.*/
{
if (this.status == 200 && this.readyState == 4)
{
document.getElementById('dispdiv').innerHTML = req.request.responseText;
}
};
req.ajaxSend();
}
*/

function Request(m, u, a, d)
{
var AjaxRequest = {
    method: m,
	url: u,
	async: a,
	data: d,
	rscAction: function() {},
	request: new XMLHttpRequest(),
    ajaxSend: function () {
	  this.rscAction();
		  this.request.open(this.method, this.url, this.async);
		  if (this.data)
		  {
		  this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          //this.request.setRequestHeader("Content-length", this.data.length);
          //this.request.setRequestHeader("Connection", "close");
		  this.request.send(this.data);
		  }
		  else
		  {
		  this.request.send();
		  }
    }
}
return AjaxRequest;
}

I think that it's pretty self-explanatory. Post or PM me if you all need more or need help.


Peace!

-Stev

Its good that you are sharing useful stuffs for newbies, but you can post such things as "code snippets" rather then to regular posts.

Its good that you are sharing useful stuffs for newbies, but you can post such things as "code snippets" rather then to regular posts.

Oh! I didn't know. Should I post there as well, or just leave it?

In future if you are going to post code and not a problem, then you should post it as code snippets.

Now you can not change your current post category, so leave it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.