Attachment 'simpleAjaxRequest.js'
Download 1 /*
2 * simpleAjaxRequest.js
3 *
4 * This is a simple AJAX request toolkit. It does not permit pipelining.
5 * In other words you need to wait for a response before doing a succeeding
6 * request.
7 *
8 * Usage: AJAX.init(url, method, json, callback)
9 * url: The URL where the AJAX response will come from.
10 * Can be: url = document.location.href + "something/";
11 * method: The HTTP method such as GET, POST, PUT, etc.
12 * json: When no data is needed this should be 'null' else a JSON
13 * object. The object can be retrieved with the 'json' keyword.
14 * callback: The callback function that does the work in your code. It
15 * takes two arguments, the request and JSON objects.
16 *
17 * Note: 1) If you expect a 100 (continue) or a 204 (no content) response you
18 * must define the appropriate callback with the same arguments as
19 * above, and set the names as shown below:
20 * AJAX.CONTINUE_CB = <continueCB function>
21 * AJAX.NO_CONTENT_CB = <noContentCB function>
22 *
23 * 2) You should also define a callback with two arguments
24 * (request, text), to handle exceptions:
25 * AJAX.EXCEPTION_CD = <exceptionCB function>
26 *
27 * ----------------------------------
28 * $Author: $
29 * $Date: $
30 * $Revision: $
31 * ----------------------------------
32 */
33
34 Function.prototype.bind = function(object) {
35 var method = this;
36 return function () {
37 method.apply(object, arguments);
38 };
39 }
40
41 var AJAX = {
42 _callback: null,
43 _request: null,
44 EXCEPTION_CB: null,
45 CONTINUE_CB: null,
46 NO_CONTENT_CB: null,
47
48 init: function(url, method, json, callback) {
49 this._callback = callback;
50 this._request = AJAX._ajaxRequest();
51 AJAX._processRequest(url, method, json);
52 },
53
54 _ajaxRequest: function() {
55 var request = null;
56
57 try {
58 request = new XMLHttpRequest();
59 } catch (e) {
60 try {
61 request = new ActiveXObject("Msxml2.XMLHTTP");
62 } catch (e) {
63 try {
64 request = new ActiveXObject("Microsoft.XMLHTTP");
65 } catch (e) {
66 }
67 }
68 }
69
70 return request;
71 },
72
73 _processRequest: function(url, method, json) {
74 if(this._request == null) {
75 msg = "_processRequest: Invalid XMLHttpRequest object";
76 this.EXCEPTION_CB(this._request, msg);
77 }
78
79 method = method.toUpperCase();
80 var query = null;
81
82 if(json != null) {
83 query = "";
84
85 if(method == "GET") {
86 query += url.match(/\?/) ? "&" : "?";
87 }
88
89 query += "json=" + escape(json);
90
91 if(method == "GET") {
92 url += query;
93 query = null;
94 }
95 }
96
97 this._request.onreadystatechange = this._handleResponse.bind(this);
98 this._request.open(method, url, true);
99
100 if(method == "POST") {
101 this._request.setRequestHeader("Content-type",
102 "application/x-www-form-urlencoded");
103 }
104
105 this._request.send(query);
106 },
107
108 _handleResponse: function() {
109 if(this._request != null && this._request.readyState == 4) {
110 var status = this._request.status;
111 var response = null;
112 var json = this._request.responseText;
113
114 if(status == 200 || status == 201) {
115 if(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(json)) {
116 json = eval("(" + json + ")");
117 this._callback(this._request, json);
118 } else {
119 msg = "_handleResponse: Invalid JSON Request: " + json;
120 this.EXCEPTION_CB(this._request, msg);
121 }
122 } else if(status == 100) {
123 if(this.CONTINUE_CB) {
124 this.CONTINUE_CB();
125 } else {
126 msg = "A callback for 100 'Continue' has not been defined.";
127 this.EXCEPTION_CB(this._request, msg);
128 }
129 } else if(status == 204) {
130 if(this.NO_CONTENT_CB) {
131 this.NO_CONTENT_CB();
132 } else {
133 msg = "A callback for 204 'No Content' has not been defined.";
134 this.EXCEPTION_CB(this._request, msg);
135 }
136 } else {
137 msg = "Error while retrieving result from the server: " +
138 (status ? "(" + status : "undefined") + ") " +
139 this._request.statusText;
140 this.EXCEPTION_CB(this._request, msg);
141 }
142 }
143 }
144 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.