A universal class for easy Ajax development

Source: Internet
Author: User
Tags object empty reserved return string
Ajax

Name:ajaxrequest
Author:hotheart (Xujiwei)
site:http://www.xujiwei.cn/
blog:http://www.xujiwei.cn/blog/
Copyright (c) 2006, all Rights Reserved
Class Name: Ajaxrequest
Version: 0.3
Date: 2006-12-18
Introduction: Ajaxrequest is a convenient AJAX development of a common class, you can easily do some of the operations needed in Ajax, so as to simplify the development steps, reduce the number of repetitive code writing.
To create a method:
var ajaxobj=new ajaxrequest ([Url],[callback],[content],[method],[async]);
Returns False if the creation fails
Property:
URL-Request URL, string, default is NULL
Callback-callback function, that is, the function called when the response content is returned, the default is direct return, and the callback function has a parameter of XMLHttpRequest object, that is, when defining a callback function: function Mycallback (xmlobj)
Content-the contents of the request, if this attribute is set by the request method for post, the default is an empty string
Methods-Request method, String, post or get, default to post
Async-Asynchronous, true to asynchronous, false to synchronous, default to True
Method
function send ([Url],[callback],[content],[method],[async])
To send a request, the optional argument list is empty to use the object property
function get ([Url],[callback])
Using the Get method to request a URL, the optional parameter uses the object property by default
function post (Form_obj,[callback],[url],[method])
Sends a form to the specified url,form_obj for the specified Form object, and the optional argument is empty when the object property is used
Example:
1. Get method
function Test1 () {
var ajax=new ajaxrequest;
Ajax.get (
"Test.asp",
function (obj) {
document.getElementById ("Test1"). Value=obj.responsetext;
}
);
}
2. Post method
function Test2 () {
var ajax=new ajaxrequest;
Ajax.post (
document.getElementById ("TEST2C"),
function (obj) {
document.getElementById ("TEST2R"). Innerhtml=obj.responsetext;
}
);
}
[Copy this Code] CODE:
/*------------------------------------------
Author:xujiwei
website:http://www.xujiwei.cn
E-mail:vipxjw@163.com
Copyright (c) 2006, all Rights Reserved
------------------------------------------*/
function Ajaxrequest () {
var xmlobj = false;
var cbfunc,objself;
Objself=this;
try {xmlobj=new XMLHttpRequest;}
catch (e) {
try {xmlobj=new ActiveXObject ("MSXML2. XMLHTTP "); }
catch (E2) {
try {xmlobj=new ActiveXObject ("Microsoft.XMLHTTP");}
catch (E3) {Xmlobj=false}
}
}
if (!xmlobj) return false;
if (Arguments[0]) this.url=arguments[0]; else This.url= "";
if (Arguments[1]) this.callback=arguments[1]; else This.callback=function (obj) {return};
if (arguments[2]) this.content=arguments[2]; else this.content= "";
if (arguments[3]) this.method=arguments[3]; else this.method= "POST";
if (Arguments[4]) this.async=arguments[4]; else this.async=true;
This.send=function () {
var Purl,pcbf,pc,pm,pa;
if (Arguments[0]) purl=arguments[0]; else Purl=this.url;
if (Arguments[1]) pc=arguments[1]; else pc=this.content;
if (arguments[2]) pcbf=arguments[2]; else Pcbf=this.callback;
if (arguments[3]) pm=arguments[3]; else Pm=this.method;
if (Arguments[4]) pa=arguments[4]; else Pa=this.async;
if (!pm| |! purl| |! PA) return false;
Xmlobj.open (PM, Purl, PA);
if (pm== "POST") Xmlobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlobj.onreadystatechange=function () {
if (xmlobj.readystate==4) {
if (xmlobj.status==200) {
PCBF (Xmlobj);
}
else {
PCBF (NULL);
}
}
}
if (pm== "POST")
Xmlobj.send (PC);
Else
Xmlobj.send ("");
}
This.get=function () {
var PURL,PCBF;
if (Arguments[0]) purl=arguments[0]; else Purl=this.url;
if (Arguments[1]) pcbf=arguments[1]; else Pcbf=this.callback;
if (!PURL&&!PCBF) return false;
This.send (Purl, "", PCBF, "get", true);
}
This.post=function () {
var fo,pcbf,purl,pc,pm;
if (Arguments[0]) fo=arguments[0]; else return false;
if (Arguments[1]) pcbf=arguments[1]; else Pcbf=this.callback;
if (arguments[2])
PURL=ARGUMENTS[2];
else if (fo.action)
Purl=fo.action;
Else
Purl=this.url;
if (Arguments[3])
PM=ARGUMENTS[3];
else if (Fo.method)
Pm=fo.method.tolowercase ();
Else
Pm= "POST";
if (!pcbf&&!purl) return false;
PC=THIS.FORMTOSTR (FO);
if (!PC) return false;
if (PM) {
if (pm== "POST")
This.send (PURL,PC,PCBF, "POST", true);
Else
if (Purl.indexof ("?") >0)
This.send (purl+ "&" +pc, "", PCBF, "get", true);
Else
This.send (purl+ "?") +PC, "", PCBF, "get", true);
}
Else
This.send (PURL,PC,PCBF, "POST", true);
}
Formtostr
From Surfchen
@url http://www.surfchen.org/
@license http://www.gnu.org/licenses/gpl.html GPL
Modified by Xujiwei
@url http://www.xujiwei.cn/
This.formtostr=function (FC) {
var i,query_string= "", and= "";
for (i=0;iE=fc[i];
if (e.name!= ') {
if (e.type== ' Select-one ') {
Element_value=e.options[e.selectedindex].value;
}
else if (e.type== ' checkbox ' | | e.type== ' Radio ') {
if (E.checked==false) {
Continue
}
Element_value=e.value;
}
else {
Element_value=e.value;
}
Element_value=encodeuricomponent (Element_value);
query_string+=and+e.name+ ' = ' +element_value;
And= "&";
}
}
return query_string;
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.