0

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

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by urtrivedi
0

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

Edited by urtrivedi: n/a

0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.