1 | define([ |
---|
2 | "./kernel", |
---|
3 | "./sniff", |
---|
4 | "require", |
---|
5 | "../io-query", |
---|
6 | /*===== "./declare", =====*/ |
---|
7 | "../dom", |
---|
8 | "../dom-form", |
---|
9 | "./Deferred", |
---|
10 | "./config", |
---|
11 | "./json", |
---|
12 | "./lang", |
---|
13 | "./array", |
---|
14 | "../on", |
---|
15 | "../aspect", |
---|
16 | "../request/watch", |
---|
17 | "../request/xhr", |
---|
18 | "../request/util" |
---|
19 | ], function(dojo, has, require, ioq, /*===== declare, =====*/ dom, domForm, Deferred, config, json, lang, array, on, aspect, watch, _xhr, util){ |
---|
20 | // module: |
---|
21 | // dojo/_base/xhr |
---|
22 | |
---|
23 | /*===== |
---|
24 | dojo._xhrObj = function(){ |
---|
25 | // summary: |
---|
26 | // does the work of portably generating a new XMLHTTPRequest object. |
---|
27 | }; |
---|
28 | =====*/ |
---|
29 | dojo._xhrObj = _xhr._create; |
---|
30 | |
---|
31 | var cfg = dojo.config; |
---|
32 | |
---|
33 | // mix in io-query and dom-form |
---|
34 | dojo.objectToQuery = ioq.objectToQuery; |
---|
35 | dojo.queryToObject = ioq.queryToObject; |
---|
36 | dojo.fieldToObject = domForm.fieldToObject; |
---|
37 | dojo.formToObject = domForm.toObject; |
---|
38 | dojo.formToQuery = domForm.toQuery; |
---|
39 | dojo.formToJson = domForm.toJson; |
---|
40 | |
---|
41 | // need to block async callbacks from snatching this thread as the result |
---|
42 | // of an async callback might call another sync XHR, this hangs khtml forever |
---|
43 | // must checked by watchInFlight() |
---|
44 | |
---|
45 | dojo._blockAsync = false; |
---|
46 | |
---|
47 | // MOW: remove dojo._contentHandlers alias in 2.0 |
---|
48 | var handlers = dojo._contentHandlers = dojo.contentHandlers = { |
---|
49 | // summary: |
---|
50 | // A map of available XHR transport handle types. Name matches the |
---|
51 | // `handleAs` attribute passed to XHR calls. |
---|
52 | // description: |
---|
53 | // A map of available XHR transport handle types. Name matches the |
---|
54 | // `handleAs` attribute passed to XHR calls. Each contentHandler is |
---|
55 | // called, passing the xhr object for manipulation. The return value |
---|
56 | // from the contentHandler will be passed to the `load` or `handle` |
---|
57 | // functions defined in the original xhr call. |
---|
58 | // example: |
---|
59 | // Creating a custom content-handler: |
---|
60 | // | xhr.contentHandlers.makeCaps = function(xhr){ |
---|
61 | // | return xhr.responseText.toUpperCase(); |
---|
62 | // | } |
---|
63 | // | // and later: |
---|
64 | // | dojo.xhrGet({ |
---|
65 | // | url:"foo.txt", |
---|
66 | // | handleAs:"makeCaps", |
---|
67 | // | load: function(data){ /* data is a toUpper version of foo.txt */ } |
---|
68 | // | }); |
---|
69 | |
---|
70 | "text": function(xhr){ |
---|
71 | // summary: |
---|
72 | // A contentHandler which simply returns the plaintext response data |
---|
73 | return xhr.responseText; |
---|
74 | }, |
---|
75 | "json": function(xhr){ |
---|
76 | // summary: |
---|
77 | // A contentHandler which returns a JavaScript object created from the response data |
---|
78 | return json.fromJson(xhr.responseText || null); |
---|
79 | }, |
---|
80 | "json-comment-filtered": function(xhr){ |
---|
81 | // summary: |
---|
82 | // A contentHandler which expects comment-filtered JSON. |
---|
83 | // description: |
---|
84 | // A contentHandler which expects comment-filtered JSON. |
---|
85 | // the json-comment-filtered option was implemented to prevent |
---|
86 | // "JavaScript Hijacking", but it is less secure than standard JSON. Use |
---|
87 | // standard JSON instead. JSON prefixing can be used to subvert hijacking. |
---|
88 | // |
---|
89 | // Will throw a notice suggesting to use application/json mimetype, as |
---|
90 | // json-commenting can introduce security issues. To decrease the chances of hijacking, |
---|
91 | // use the standard `json` contentHandler, and prefix your "JSON" with: {}&& |
---|
92 | // |
---|
93 | // use djConfig.useCommentedJson = true to turn off the notice |
---|
94 | if(!config.useCommentedJson){ |
---|
95 | console.warn("Consider using the standard mimetype:application/json." |
---|
96 | + " json-commenting can introduce security issues. To" |
---|
97 | + " decrease the chances of hijacking, use the standard the 'json' handler and" |
---|
98 | + " prefix your json with: {}&&\n" |
---|
99 | + "Use djConfig.useCommentedJson=true to turn off this message."); |
---|
100 | } |
---|
101 | |
---|
102 | var value = xhr.responseText; |
---|
103 | var cStartIdx = value.indexOf("\/*"); |
---|
104 | var cEndIdx = value.lastIndexOf("*\/"); |
---|
105 | if(cStartIdx == -1 || cEndIdx == -1){ |
---|
106 | throw new Error("JSON was not comment filtered"); |
---|
107 | } |
---|
108 | return json.fromJson(value.substring(cStartIdx+2, cEndIdx)); |
---|
109 | }, |
---|
110 | "javascript": function(xhr){ |
---|
111 | // summary: |
---|
112 | // A contentHandler which evaluates the response data, expecting it to be valid JavaScript |
---|
113 | |
---|
114 | // FIXME: try Moz and IE specific eval variants? |
---|
115 | return dojo.eval(xhr.responseText); |
---|
116 | }, |
---|
117 | "xml": function(xhr){ |
---|
118 | // summary: |
---|
119 | // A contentHandler returning an XML Document parsed from the response data |
---|
120 | var result = xhr.responseXML; |
---|
121 | |
---|
122 | if(result && has("dom-qsa2.1") && !result.querySelectorAll && has("dom-parser")){ |
---|
123 | // http://bugs.dojotoolkit.org/ticket/15631 |
---|
124 | // IE9 supports a CSS3 querySelectorAll implementation, but the DOM implementation |
---|
125 | // returned by IE9 xhr.responseXML does not. Manually create the XML DOM to gain |
---|
126 | // the fuller-featured implementation and avoid bugs caused by the inconsistency |
---|
127 | result = new DOMParser().parseFromString(xhr.responseText, "application/xml"); |
---|
128 | } |
---|
129 | |
---|
130 | if(has("ie")){ |
---|
131 | if((!result || !result.documentElement)){ |
---|
132 | //WARNING: this branch used by the xml handling in dojo.io.iframe, |
---|
133 | //so be sure to test dojo.io.iframe if making changes below. |
---|
134 | var ms = function(n){ return "MSXML" + n + ".DOMDocument"; }; |
---|
135 | var dp = ["Microsoft.XMLDOM", ms(6), ms(4), ms(3), ms(2)]; |
---|
136 | array.some(dp, function(p){ |
---|
137 | try{ |
---|
138 | var dom = new ActiveXObject(p); |
---|
139 | dom.async = false; |
---|
140 | dom.loadXML(xhr.responseText); |
---|
141 | result = dom; |
---|
142 | }catch(e){ return false; } |
---|
143 | return true; |
---|
144 | }); |
---|
145 | } |
---|
146 | } |
---|
147 | return result; // DOMDocument |
---|
148 | }, |
---|
149 | "json-comment-optional": function(xhr){ |
---|
150 | // summary: |
---|
151 | // A contentHandler which checks the presence of comment-filtered JSON and |
---|
152 | // alternates between the `json` and `json-comment-filtered` contentHandlers. |
---|
153 | if(xhr.responseText && /^[^{\[]*\/\*/.test(xhr.responseText)){ |
---|
154 | return handlers["json-comment-filtered"](xhr); |
---|
155 | }else{ |
---|
156 | return handlers["json"](xhr); |
---|
157 | } |
---|
158 | } |
---|
159 | }; |
---|
160 | |
---|
161 | /*===== |
---|
162 | |
---|
163 | // kwargs function parameter definitions. Assigning to dojo namespace rather than making them local variables |
---|
164 | // because they are used by dojo/io modules too |
---|
165 | |
---|
166 | dojo.__IoArgs = declare(null, { |
---|
167 | // url: String |
---|
168 | // URL to server endpoint. |
---|
169 | // content: Object? |
---|
170 | // Contains properties with string values. These |
---|
171 | // properties will be serialized as name1=value2 and |
---|
172 | // passed in the request. |
---|
173 | // timeout: Integer? |
---|
174 | // Milliseconds to wait for the response. If this time |
---|
175 | // passes, the then error callbacks are called. |
---|
176 | // form: DOMNode? |
---|
177 | // DOM node for a form. Used to extract the form values |
---|
178 | // and send to the server. |
---|
179 | // preventCache: Boolean? |
---|
180 | // Default is false. If true, then a |
---|
181 | // "dojo.preventCache" parameter is sent in the request |
---|
182 | // with a value that changes with each request |
---|
183 | // (timestamp). Useful only with GET-type requests. |
---|
184 | // handleAs: String? |
---|
185 | // Acceptable values depend on the type of IO |
---|
186 | // transport (see specific IO calls for more information). |
---|
187 | // rawBody: String? |
---|
188 | // Sets the raw body for an HTTP request. If this is used, then the content |
---|
189 | // property is ignored. This is mostly useful for HTTP methods that have |
---|
190 | // a body to their requests, like PUT or POST. This property can be used instead |
---|
191 | // of postData and putData for dojo/_base/xhr.rawXhrPost and dojo/_base/xhr.rawXhrPut respectively. |
---|
192 | // ioPublish: Boolean? |
---|
193 | // Set this explicitly to false to prevent publishing of topics related to |
---|
194 | // IO operations. Otherwise, if djConfig.ioPublish is set to true, topics |
---|
195 | // will be published via dojo/topic.publish() for different phases of an IO operation. |
---|
196 | // See dojo/main.__IoPublish for a list of topics that are published. |
---|
197 | |
---|
198 | load: function(response, ioArgs){ |
---|
199 | // summary: |
---|
200 | // This function will be |
---|
201 | // called on a successful HTTP response code. |
---|
202 | // ioArgs: dojo/main.__IoCallbackArgs |
---|
203 | // Provides additional information about the request. |
---|
204 | // response: Object |
---|
205 | // The response in the format as defined with handleAs. |
---|
206 | }, |
---|
207 | |
---|
208 | error: function(response, ioArgs){ |
---|
209 | // summary: |
---|
210 | // This function will |
---|
211 | // be called when the request fails due to a network or server error, the url |
---|
212 | // is invalid, etc. It will also be called if the load or handle callback throws an |
---|
213 | // exception, unless djConfig.debugAtAllCosts is true. This allows deployed applications |
---|
214 | // to continue to run even when a logic error happens in the callback, while making |
---|
215 | // it easier to troubleshoot while in debug mode. |
---|
216 | // ioArgs: dojo/main.__IoCallbackArgs |
---|
217 | // Provides additional information about the request. |
---|
218 | // response: Object |
---|
219 | // The response in the format as defined with handleAs. |
---|
220 | }, |
---|
221 | |
---|
222 | handle: function(loadOrError, response, ioArgs){ |
---|
223 | // summary: |
---|
224 | // This function will |
---|
225 | // be called at the end of every request, whether or not an error occurs. |
---|
226 | // loadOrError: String |
---|
227 | // Provides a string that tells you whether this function |
---|
228 | // was called because of success (load) or failure (error). |
---|
229 | // response: Object |
---|
230 | // The response in the format as defined with handleAs. |
---|
231 | // ioArgs: dojo/main.__IoCallbackArgs |
---|
232 | // Provides additional information about the request. |
---|
233 | } |
---|
234 | }); |
---|
235 | |
---|
236 | dojo.__IoCallbackArgs = declare(null, { |
---|
237 | // args: Object |
---|
238 | // the original object argument to the IO call. |
---|
239 | // xhr: XMLHttpRequest |
---|
240 | // For XMLHttpRequest calls only, the |
---|
241 | // XMLHttpRequest object that was used for the |
---|
242 | // request. |
---|
243 | // url: String |
---|
244 | // The final URL used for the call. Many times it |
---|
245 | // will be different than the original args.url |
---|
246 | // value. |
---|
247 | // query: String |
---|
248 | // For non-GET requests, the |
---|
249 | // name1=value1&name2=value2 parameters sent up in |
---|
250 | // the request. |
---|
251 | // handleAs: String |
---|
252 | // The final indicator on how the response will be |
---|
253 | // handled. |
---|
254 | // id: String |
---|
255 | // For dojo/io/script calls only, the internal |
---|
256 | // script ID used for the request. |
---|
257 | // canDelete: Boolean |
---|
258 | // For dojo/io/script calls only, indicates |
---|
259 | // whether the script tag that represents the |
---|
260 | // request can be deleted after callbacks have |
---|
261 | // been called. Used internally to know when |
---|
262 | // cleanup can happen on JSONP-type requests. |
---|
263 | // json: Object |
---|
264 | // For dojo/io/script calls only: holds the JSON |
---|
265 | // response for JSONP-type requests. Used |
---|
266 | // internally to hold on to the JSON responses. |
---|
267 | // You should not need to access it directly -- |
---|
268 | // the same object should be passed to the success |
---|
269 | // callbacks directly. |
---|
270 | }); |
---|
271 | |
---|
272 | dojo.__IoPublish = declare(null, { |
---|
273 | // summary: |
---|
274 | // This is a list of IO topics that can be published |
---|
275 | // if djConfig.ioPublish is set to true. IO topics can be |
---|
276 | // published for any Input/Output, network operation. So, |
---|
277 | // dojo.xhr, dojo.io.script and dojo.io.iframe can all |
---|
278 | // trigger these topics to be published. |
---|
279 | // start: String |
---|
280 | // "/dojo/io/start" is sent when there are no outstanding IO |
---|
281 | // requests, and a new IO request is started. No arguments |
---|
282 | // are passed with this topic. |
---|
283 | // send: String |
---|
284 | // "/dojo/io/send" is sent whenever a new IO request is started. |
---|
285 | // It passes the dojo.Deferred for the request with the topic. |
---|
286 | // load: String |
---|
287 | // "/dojo/io/load" is sent whenever an IO request has loaded |
---|
288 | // successfully. It passes the response and the dojo.Deferred |
---|
289 | // for the request with the topic. |
---|
290 | // error: String |
---|
291 | // "/dojo/io/error" is sent whenever an IO request has errored. |
---|
292 | // It passes the error and the dojo.Deferred |
---|
293 | // for the request with the topic. |
---|
294 | // done: String |
---|
295 | // "/dojo/io/done" is sent whenever an IO request has completed, |
---|
296 | // either by loading or by erroring. It passes the error and |
---|
297 | // the dojo.Deferred for the request with the topic. |
---|
298 | // stop: String |
---|
299 | // "/dojo/io/stop" is sent when all outstanding IO requests have |
---|
300 | // finished. No arguments are passed with this topic. |
---|
301 | }); |
---|
302 | =====*/ |
---|
303 | |
---|
304 | |
---|
305 | dojo._ioSetArgs = function(/*dojo/main.__IoArgs*/args, |
---|
306 | /*Function*/canceller, |
---|
307 | /*Function*/okHandler, |
---|
308 | /*Function*/errHandler){ |
---|
309 | // summary: |
---|
310 | // sets up the Deferred and ioArgs property on the Deferred so it |
---|
311 | // can be used in an io call. |
---|
312 | // args: |
---|
313 | // The args object passed into the public io call. Recognized properties on |
---|
314 | // the args object are: |
---|
315 | // canceller: |
---|
316 | // The canceller function used for the Deferred object. The function |
---|
317 | // will receive one argument, the Deferred object that is related to the |
---|
318 | // canceller. |
---|
319 | // okHandler: |
---|
320 | // The first OK callback to be registered with Deferred. It has the opportunity |
---|
321 | // to transform the OK response. It will receive one argument -- the Deferred |
---|
322 | // object returned from this function. |
---|
323 | // errHandler: |
---|
324 | // The first error callback to be registered with Deferred. It has the opportunity |
---|
325 | // to do cleanup on an error. It will receive two arguments: error (the |
---|
326 | // Error object) and dfd, the Deferred object returned from this function. |
---|
327 | |
---|
328 | var ioArgs = {args: args, url: args.url}; |
---|
329 | |
---|
330 | //Get values from form if requested. |
---|
331 | var formObject = null; |
---|
332 | if(args.form){ |
---|
333 | var form = dom.byId(args.form); |
---|
334 | //IE requires going through getAttributeNode instead of just getAttribute in some form cases, |
---|
335 | //so use it for all. See #2844 |
---|
336 | var actnNode = form.getAttributeNode("action"); |
---|
337 | ioArgs.url = ioArgs.url || (actnNode ? actnNode.value : null); |
---|
338 | formObject = domForm.toObject(form); |
---|
339 | } |
---|
340 | |
---|
341 | // set up the query params |
---|
342 | var miArgs = [{}]; |
---|
343 | |
---|
344 | if(formObject){ |
---|
345 | // potentially over-ride url-provided params w/ form values |
---|
346 | miArgs.push(formObject); |
---|
347 | } |
---|
348 | if(args.content){ |
---|
349 | // stuff in content over-rides what's set by form |
---|
350 | miArgs.push(args.content); |
---|
351 | } |
---|
352 | if(args.preventCache){ |
---|
353 | miArgs.push({"dojo.preventCache": new Date().valueOf()}); |
---|
354 | } |
---|
355 | ioArgs.query = ioq.objectToQuery(lang.mixin.apply(null, miArgs)); |
---|
356 | |
---|
357 | // .. and the real work of getting the deferred in order, etc. |
---|
358 | ioArgs.handleAs = args.handleAs || "text"; |
---|
359 | var d = new Deferred(function(dfd){ |
---|
360 | dfd.canceled = true; |
---|
361 | canceller && canceller(dfd); |
---|
362 | |
---|
363 | var err = dfd.ioArgs.error; |
---|
364 | if(!err){ |
---|
365 | err = new Error("request cancelled"); |
---|
366 | err.dojoType="cancel"; |
---|
367 | dfd.ioArgs.error = err; |
---|
368 | } |
---|
369 | return err; |
---|
370 | }); |
---|
371 | d.addCallback(okHandler); |
---|
372 | |
---|
373 | //Support specifying load, error and handle callback functions from the args. |
---|
374 | //For those callbacks, the "this" object will be the args object. |
---|
375 | //The callbacks will get the deferred result value as the |
---|
376 | //first argument and the ioArgs object as the second argument. |
---|
377 | var ld = args.load; |
---|
378 | if(ld && lang.isFunction(ld)){ |
---|
379 | d.addCallback(function(value){ |
---|
380 | return ld.call(args, value, ioArgs); |
---|
381 | }); |
---|
382 | } |
---|
383 | var err = args.error; |
---|
384 | if(err && lang.isFunction(err)){ |
---|
385 | d.addErrback(function(value){ |
---|
386 | return err.call(args, value, ioArgs); |
---|
387 | }); |
---|
388 | } |
---|
389 | var handle = args.handle; |
---|
390 | if(handle && lang.isFunction(handle)){ |
---|
391 | d.addBoth(function(value){ |
---|
392 | return handle.call(args, value, ioArgs); |
---|
393 | }); |
---|
394 | } |
---|
395 | |
---|
396 | // Attach error handler last (not including topic publishing) |
---|
397 | // to catch any errors that may have been generated from load |
---|
398 | // or handle functions. |
---|
399 | d.addErrback(function(error){ |
---|
400 | return errHandler(error, d); |
---|
401 | }); |
---|
402 | |
---|
403 | //Plug in topic publishing, if dojo.publish is loaded. |
---|
404 | if(cfg.ioPublish && dojo.publish && ioArgs.args.ioPublish !== false){ |
---|
405 | d.addCallbacks( |
---|
406 | function(res){ |
---|
407 | dojo.publish("/dojo/io/load", [d, res]); |
---|
408 | return res; |
---|
409 | }, |
---|
410 | function(res){ |
---|
411 | dojo.publish("/dojo/io/error", [d, res]); |
---|
412 | return res; |
---|
413 | } |
---|
414 | ); |
---|
415 | d.addBoth(function(res){ |
---|
416 | dojo.publish("/dojo/io/done", [d, res]); |
---|
417 | return res; |
---|
418 | }); |
---|
419 | } |
---|
420 | |
---|
421 | d.ioArgs = ioArgs; |
---|
422 | |
---|
423 | // FIXME: need to wire up the xhr object's abort method to something |
---|
424 | // analogous in the Deferred |
---|
425 | return d; |
---|
426 | }; |
---|
427 | |
---|
428 | var _deferredOk = function(/*Deferred*/dfd){ |
---|
429 | // summary: |
---|
430 | // okHandler function for dojo._ioSetArgs call. |
---|
431 | |
---|
432 | var ret = handlers[dfd.ioArgs.handleAs](dfd.ioArgs.xhr); |
---|
433 | return ret === undefined ? null : ret; |
---|
434 | }; |
---|
435 | var _deferError = function(/*Error*/error, /*Deferred*/dfd){ |
---|
436 | // summary: |
---|
437 | // errHandler function for dojo._ioSetArgs call. |
---|
438 | |
---|
439 | if(!dfd.ioArgs.args.failOk){ |
---|
440 | console.error(error); |
---|
441 | } |
---|
442 | return error; |
---|
443 | }; |
---|
444 | |
---|
445 | //Use a separate count for knowing if we are starting/stopping io calls. |
---|
446 | var _checkPubCount = function(dfd){ |
---|
447 | if(_pubCount <= 0){ |
---|
448 | _pubCount = 0; |
---|
449 | if(cfg.ioPublish && dojo.publish && (!dfd || dfd && dfd.ioArgs.args.ioPublish !== false)){ |
---|
450 | dojo.publish("/dojo/io/stop"); |
---|
451 | } |
---|
452 | } |
---|
453 | }; |
---|
454 | |
---|
455 | var _pubCount = 0; |
---|
456 | aspect.after(watch, "_onAction", function(){ |
---|
457 | _pubCount -= 1; |
---|
458 | }); |
---|
459 | aspect.after(watch, "_onInFlight", _checkPubCount); |
---|
460 | |
---|
461 | dojo._ioCancelAll = watch.cancelAll; |
---|
462 | /*===== |
---|
463 | dojo._ioCancelAll = function(){ |
---|
464 | // summary: |
---|
465 | // Cancels all pending IO requests, regardless of IO type |
---|
466 | // (xhr, script, iframe). |
---|
467 | }; |
---|
468 | =====*/ |
---|
469 | |
---|
470 | dojo._ioNotifyStart = function(/*Deferred*/dfd){ |
---|
471 | // summary: |
---|
472 | // If dojo.publish is available, publish topics |
---|
473 | // about the start of a request queue and/or the |
---|
474 | // the beginning of request. |
---|
475 | // |
---|
476 | // Used by IO transports. An IO transport should |
---|
477 | // call this method before making the network connection. |
---|
478 | if(cfg.ioPublish && dojo.publish && dfd.ioArgs.args.ioPublish !== false){ |
---|
479 | if(!_pubCount){ |
---|
480 | dojo.publish("/dojo/io/start"); |
---|
481 | } |
---|
482 | _pubCount += 1; |
---|
483 | dojo.publish("/dojo/io/send", [dfd]); |
---|
484 | } |
---|
485 | }; |
---|
486 | |
---|
487 | dojo._ioWatch = function(dfd, validCheck, ioCheck, resHandle){ |
---|
488 | // summary: |
---|
489 | // Watches the io request represented by dfd to see if it completes. |
---|
490 | // dfd: Deferred |
---|
491 | // The Deferred object to watch. |
---|
492 | // validCheck: Function |
---|
493 | // Function used to check if the IO request is still valid. Gets the dfd |
---|
494 | // object as its only argument. |
---|
495 | // ioCheck: Function |
---|
496 | // Function used to check if basic IO call worked. Gets the dfd |
---|
497 | // object as its only argument. |
---|
498 | // resHandle: Function |
---|
499 | // Function used to process response. Gets the dfd |
---|
500 | // object as its only argument. |
---|
501 | |
---|
502 | var args = dfd.ioArgs.options = dfd.ioArgs.args; |
---|
503 | lang.mixin(dfd, { |
---|
504 | response: dfd.ioArgs, |
---|
505 | isValid: function(response){ |
---|
506 | return validCheck(dfd); |
---|
507 | }, |
---|
508 | isReady: function(response){ |
---|
509 | return ioCheck(dfd); |
---|
510 | }, |
---|
511 | handleResponse: function(response){ |
---|
512 | return resHandle(dfd); |
---|
513 | } |
---|
514 | }); |
---|
515 | watch(dfd); |
---|
516 | |
---|
517 | _checkPubCount(dfd); |
---|
518 | }; |
---|
519 | |
---|
520 | var _defaultContentType = "application/x-www-form-urlencoded"; |
---|
521 | |
---|
522 | dojo._ioAddQueryToUrl = function(/*dojo.__IoCallbackArgs*/ioArgs){ |
---|
523 | // summary: |
---|
524 | // Adds query params discovered by the io deferred construction to the URL. |
---|
525 | // Only use this for operations which are fundamentally GET-type operations. |
---|
526 | if(ioArgs.query.length){ |
---|
527 | ioArgs.url += (ioArgs.url.indexOf("?") == -1 ? "?" : "&") + ioArgs.query; |
---|
528 | ioArgs.query = null; |
---|
529 | } |
---|
530 | }; |
---|
531 | |
---|
532 | /*===== |
---|
533 | dojo.__XhrArgs = declare(dojo.__IoArgs, { |
---|
534 | // summary: |
---|
535 | // In addition to the properties listed for the dojo._IoArgs type, |
---|
536 | // the following properties are allowed for dojo.xhr* methods. |
---|
537 | // handleAs: String? |
---|
538 | // Acceptable values are: text (default), json, json-comment-optional, |
---|
539 | // json-comment-filtered, javascript, xml. See `dojo/_base/xhr.contentHandlers` |
---|
540 | // sync: Boolean? |
---|
541 | // false is default. Indicates whether the request should |
---|
542 | // be a synchronous (blocking) request. |
---|
543 | // headers: Object? |
---|
544 | // Additional HTTP headers to send in the request. |
---|
545 | // failOk: Boolean? |
---|
546 | // false is default. Indicates whether a request should be |
---|
547 | // allowed to fail (and therefore no console error message in |
---|
548 | // the event of a failure) |
---|
549 | // contentType: String|Boolean |
---|
550 | // "application/x-www-form-urlencoded" is default. Set to false to |
---|
551 | // prevent a Content-Type header from being sent, or to a string |
---|
552 | // to send a different Content-Type. |
---|
553 | }); |
---|
554 | =====*/ |
---|
555 | |
---|
556 | dojo.xhr = function(/*String*/ method, /*dojo.__XhrArgs*/ args, /*Boolean?*/ hasBody){ |
---|
557 | // summary: |
---|
558 | // Deprecated. Use dojo/request instead. |
---|
559 | // description: |
---|
560 | // Sends an HTTP request with the given method. |
---|
561 | // See also dojo.xhrGet(), xhrPost(), xhrPut() and dojo.xhrDelete() for shortcuts |
---|
562 | // for those HTTP methods. There are also methods for "raw" PUT and POST methods |
---|
563 | // via dojo.rawXhrPut() and dojo.rawXhrPost() respectively. |
---|
564 | // method: |
---|
565 | // HTTP method to be used, such as GET, POST, PUT, DELETE. Should be uppercase. |
---|
566 | // hasBody: |
---|
567 | // If the request has an HTTP body, then pass true for hasBody. |
---|
568 | |
---|
569 | var rDfd; |
---|
570 | //Make the Deferred object for this xhr request. |
---|
571 | var dfd = dojo._ioSetArgs(args, function(dfd){ |
---|
572 | rDfd && rDfd.cancel(); |
---|
573 | }, _deferredOk, _deferError); |
---|
574 | var ioArgs = dfd.ioArgs; |
---|
575 | |
---|
576 | //Allow for specifying the HTTP body completely. |
---|
577 | if("postData" in args){ |
---|
578 | ioArgs.query = args.postData; |
---|
579 | }else if("putData" in args){ |
---|
580 | ioArgs.query = args.putData; |
---|
581 | }else if("rawBody" in args){ |
---|
582 | ioArgs.query = args.rawBody; |
---|
583 | }else if((arguments.length > 2 && !hasBody) || "POST|PUT".indexOf(method.toUpperCase()) === -1){ |
---|
584 | //Check for hasBody being passed. If no hasBody, |
---|
585 | //then only append query string if not a POST or PUT request. |
---|
586 | dojo._ioAddQueryToUrl(ioArgs); |
---|
587 | } |
---|
588 | |
---|
589 | var options = { |
---|
590 | method: method, |
---|
591 | handleAs: "text", |
---|
592 | timeout: args.timeout, |
---|
593 | withCredentials: args.withCredentials, |
---|
594 | ioArgs: ioArgs |
---|
595 | }; |
---|
596 | |
---|
597 | if(typeof args.headers !== 'undefined'){ |
---|
598 | options.headers = args.headers; |
---|
599 | } |
---|
600 | if(typeof args.contentType !== 'undefined'){ |
---|
601 | if(!options.headers){ |
---|
602 | options.headers = {}; |
---|
603 | } |
---|
604 | options.headers['Content-Type'] = args.contentType; |
---|
605 | } |
---|
606 | if(typeof ioArgs.query !== 'undefined'){ |
---|
607 | options.data = ioArgs.query; |
---|
608 | } |
---|
609 | if(typeof args.sync !== 'undefined'){ |
---|
610 | options.sync = args.sync; |
---|
611 | } |
---|
612 | |
---|
613 | dojo._ioNotifyStart(dfd); |
---|
614 | try{ |
---|
615 | rDfd = _xhr(ioArgs.url, options, true); |
---|
616 | }catch(e){ |
---|
617 | // If XHR creation fails, dojo/request/xhr throws |
---|
618 | // When this happens, cancel the deferred |
---|
619 | dfd.cancel(); |
---|
620 | return dfd; |
---|
621 | } |
---|
622 | |
---|
623 | // sync ioArgs |
---|
624 | dfd.ioArgs.xhr = rDfd.response.xhr; |
---|
625 | |
---|
626 | rDfd.then(function(){ |
---|
627 | dfd.resolve(dfd); |
---|
628 | }).otherwise(function(error){ |
---|
629 | ioArgs.error = error; |
---|
630 | if(error.response){ |
---|
631 | error.status = error.response.status; |
---|
632 | error.responseText = error.response.text; |
---|
633 | error.xhr = error.response.xhr; |
---|
634 | } |
---|
635 | dfd.reject(error); |
---|
636 | }); |
---|
637 | return dfd; // dojo/_base/Deferred |
---|
638 | }; |
---|
639 | |
---|
640 | dojo.xhrGet = function(/*dojo.__XhrArgs*/ args){ |
---|
641 | // summary: |
---|
642 | // Sends an HTTP GET request to the server. |
---|
643 | return dojo.xhr("GET", args); // dojo/_base/Deferred |
---|
644 | }; |
---|
645 | |
---|
646 | dojo.rawXhrPost = dojo.xhrPost = function(/*dojo.__XhrArgs*/ args){ |
---|
647 | // summary: |
---|
648 | // Sends an HTTP POST request to the server. In addition to the properties |
---|
649 | // listed for the dojo.__XhrArgs type, the following property is allowed: |
---|
650 | // postData: |
---|
651 | // String. Send raw data in the body of the POST request. |
---|
652 | return dojo.xhr("POST", args, true); // dojo/_base/Deferred |
---|
653 | }; |
---|
654 | |
---|
655 | dojo.rawXhrPut = dojo.xhrPut = function(/*dojo.__XhrArgs*/ args){ |
---|
656 | // summary: |
---|
657 | // Sends an HTTP PUT request to the server. In addition to the properties |
---|
658 | // listed for the dojo.__XhrArgs type, the following property is allowed: |
---|
659 | // putData: |
---|
660 | // String. Send raw data in the body of the PUT request. |
---|
661 | return dojo.xhr("PUT", args, true); // dojo/_base/Deferred |
---|
662 | }; |
---|
663 | |
---|
664 | dojo.xhrDelete = function(/*dojo.__XhrArgs*/ args){ |
---|
665 | // summary: |
---|
666 | // Sends an HTTP DELETE request to the server. |
---|
667 | return dojo.xhr("DELETE", args); // dojo/_base/Deferred |
---|
668 | }; |
---|
669 | |
---|
670 | /* |
---|
671 | dojo.wrapForm = function(formNode){ |
---|
672 | // summary: |
---|
673 | // A replacement for FormBind, but not implemented yet. |
---|
674 | |
---|
675 | // FIXME: need to think harder about what extensions to this we might |
---|
676 | // want. What should we allow folks to do w/ this? What events to |
---|
677 | // set/send? |
---|
678 | throw new Error("dojo.wrapForm not yet implemented"); |
---|
679 | } |
---|
680 | */ |
---|
681 | |
---|
682 | dojo._isDocumentOk = function(x){ |
---|
683 | return util.checkStatus(x.status); |
---|
684 | }; |
---|
685 | |
---|
686 | dojo._getText = function(url){ |
---|
687 | var result; |
---|
688 | dojo.xhrGet({url:url, sync:true, load:function(text){ |
---|
689 | result = text; |
---|
690 | }}); |
---|
691 | return result; |
---|
692 | }; |
---|
693 | |
---|
694 | // Add aliases for static functions to dojo.xhr since dojo.xhr is what's returned from this module |
---|
695 | lang.mixin(dojo.xhr, { |
---|
696 | _xhrObj: dojo._xhrObj, |
---|
697 | fieldToObject: domForm.fieldToObject, |
---|
698 | formToObject: domForm.toObject, |
---|
699 | objectToQuery: ioq.objectToQuery, |
---|
700 | formToQuery: domForm.toQuery, |
---|
701 | formToJson: domForm.toJson, |
---|
702 | queryToObject: ioq.queryToObject, |
---|
703 | contentHandlers: handlers, |
---|
704 | _ioSetArgs: dojo._ioSetArgs, |
---|
705 | _ioCancelAll: dojo._ioCancelAll, |
---|
706 | _ioNotifyStart: dojo._ioNotifyStart, |
---|
707 | _ioWatch: dojo._ioWatch, |
---|
708 | _ioAddQueryToUrl: dojo._ioAddQueryToUrl, |
---|
709 | _isDocumentOk: dojo._isDocumentOk, |
---|
710 | _getText: dojo._getText, |
---|
711 | get: dojo.xhrGet, |
---|
712 | post: dojo.xhrPost, |
---|
713 | put: dojo.xhrPut, |
---|
714 | del: dojo.xhrDelete // because "delete" is a reserved word |
---|
715 | }); |
---|
716 | |
---|
717 | return dojo.xhr; |
---|
718 | }); |
---|