Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a general class developed by AJAX

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to write the general class of AJAX development". In the daily operation, I believe that many people have doubts about how to write the general class of AJAX development. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to write the general class of AJAX development". Next, please follow the editor to study!

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 general class convenient for AJAX development, which can easily carry out some operations needed in AJAX, so as to simplify the development steps and reduce the amount of repetitive code writing.

Creation method:

Var ajaxobj=new AJAXRequest ([url], [callback], [content], [method], [async])

Return false if creation fails

Attributes:

Url-request URL, string, empty by default

Callback-callback function, that is, the function that is called when the response content is returned. By default, the callback function is returned directly. The callback function has a parameter of XMLHttpRequest object, that is, the callback function should be defined as follows: function mycallback (xmlobj)

Content-the content of the request. If the request method is POST, this property needs to be set. Default is an empty string.

Method-request method, string, POST or GET. Default is POST.

Async-whether asynchronous. True is asynchronous, false is synchronous. Default is true.

Method

Function send ([url], [callback], [content], [method], [async])

To send a request, use object properties if the optional parameter list is empty

Function get ([url], [callback])

Request a URL using the GET method. Optional parameters use object properties by default.

Function post (form_obj, [callback], [url], [method])

Send a form to the specified URL,form_obj as the specified form object, using object properties when the optional parameter is empty

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") [xss_clean] = obj.responseText

}

);

}

The code is as follows:

/ *--

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 (! purlingly loaded 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) 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 iQuery query strings = "", and= ""

For (iSuppli)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report