root/ajaxsample/MyAjax.class.php @ 70

Revision 70, 1.9 kB (checked in by d0nut, 5 years ago)

who needs tagging and branches for docs?

Line 
1/**
2 * MyAjax Klasse
3 */
4function MyAjax() {
5        this.xmlHttpRequest = null;
6
7        /**
8         * Sendet eine AJAX GET Anfrage und verknÃŒpft die Antwort mit der Callback Funktion
9         * @param url                   string
10         * @param myCallback            reference
11         */
12        this.get = function(url, myCallback) {
13                if (this.openXMLHttpRequest(myCallback)) {
14                        this.xmlHttpRequest.open('GET', url, true);
15                        this.xmlHttpRequest.send(null);
16                        return true;
17                }
18                return false;
19        }
20
21        /**
22         * Sendet eine AJAX Post Anfrage und verknÃŒpft die Antwort mit der Callback Funktion
23         * @param url                   string
24         * @param postData              string
25         * @param myCallback            reference
26         */
27        this.post = function(url, postData, myCallback) {
28                if (this.openXMLHttpRequest(myCallback)) {
29                        this.xmlHttpRequest.open('POST', url, true);
30                        this.xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
31                        this.xmlHttpRequest.send(postData);
32                        return true;
33                }
34                return false;
35        }
36
37        /**
38         * erstellt ein lokales XMLHttpRequest Objekt
39         * @param myCallback            reference
40         */
41        this.openXMLHttpRequest = function(myCallback) {
42                if (this.xmlHttpRequest) {
43                        if (this.xmlHttpRequest.readyState != 0 && this.xmlHttpRequest.readyState != 4) {
44                                return false;
45                        }
46                        this.xmlHttpRequest.abort();
47                }
48       
49                // Internet Explorer
50                try {
51                        this.xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
52                } catch (e) {
53                        try {
54                                this.xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
55                        } catch (e) {
56                                this.xmlHttpRequest  = null;
57                        }
58                }
59       
60                // Mozilla, Opera und Safari
61                if (!this.xmlHttpRequest) {
62                        if (typeof XMLHttpRequest != 'undefined') {
63                                this.xmlHttpRequest = new XMLHttpRequest();
64                                if (this.xmlHttpRequest.overrideMimeType) {
65                                        this.xmlHttpRequest.overrideMimeType('text/xml');
66                                }
67                        } else {
68                                return false;
69                        }
70                }
71       
72                // fÃŒgt unsere Callback Funktion als EventListener hinzu
73                this.xmlHttpRequest.onreadystatechange = myCallback;
74       
75                return true;
76        }
77}
Note: See TracBrowser for help on using the browser.