| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A utility for retrieving data from a URL. | 8 * A utility for retrieving data from a URL. |
| 9 * | 9 * |
| 10 * HttpRequest can be used to obtain data from http, ftp, and file | 10 * HttpRequest can be used to obtain data from http, ftp, and file |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * with HttpRequest. However, there are ways to | 23 * with HttpRequest. However, there are ways to |
| 24 * [get around this restriction](http://www.dartlang.org/articles/json-web-servi
ce/#note-on-jsonp). | 24 * [get around this restriction](http://www.dartlang.org/articles/json-web-servi
ce/#note-on-jsonp). |
| 25 * | 25 * |
| 26 * See also: | 26 * See also: |
| 27 * | 27 * |
| 28 * * [Dart article on using HttpRequests](http://www.dartlang.org/articles/json-
web-service/#getting-data) | 28 * * [Dart article on using HttpRequests](http://www.dartlang.org/articles/json-
web-service/#getting-data) |
| 29 * * [JS XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpReq
uest) | 29 * * [JS XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpReq
uest) |
| 30 * * [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttp
Request/Using_XMLHttpRequest) | 30 * * [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttp
Request/Using_XMLHttpRequest) |
| 31 */ | 31 */ |
| 32 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 32 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 33 /** |
| 34 * Creates a URL get request for the specified `url`. |
| 35 * |
| 36 * After completing the request, the object will call the user-provided |
| 37 * [onComplete] callback. |
| 38 */ |
| 39 factory $CLASSNAME.get(String url, onComplete($CLASSNAME request)) => |
| 40 _HttpRequestUtils.get(url, onComplete, false); |
| 33 | 41 |
| 42 // 80 char issue for comments in lists: dartbug.com/7588. |
| 34 /** | 43 /** |
| 35 * Creates a URL get request for the specified [url]. | 44 * Creates a URL GET request for the specified `url` with |
| 45 * credentials such a cookie (already) set in the header or |
| 46 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2). |
| 36 * | 47 * |
| 37 * The server response must be a `text/` mime type for this request to | 48 * After completing the request, the object will call the user-provided |
| 38 * succeed. | 49 * [onComplete] callback. |
| 39 * | 50 * |
| 40 * This is similar to [request] but specialized for HTTP GET requests which | 51 * A few other details to keep in mind when using credentials: |
| 41 * return text content. | |
| 42 * | |
| 43 * See also: | |
| 44 * | |
| 45 * * [request] | |
| 46 */ | |
| 47 static Future<String> getString(String url, | |
| 48 {bool withCredentials, void onProgress(ProgressEvent e)}) { | |
| 49 return request(url, withCredentials: withCredentials, | |
| 50 onProgress: onProgress).then((xhr) => xhr.responseText); | |
| 51 } | |
| 52 | |
| 53 /** | |
| 54 * Creates a URL request for the specified [url]. | |
| 55 * | |
| 56 * By default this will do an HTTP GET request, this can be overridden with | |
| 57 * [method]. | |
| 58 * | |
| 59 * The Future is completed when the response is available. | |
| 60 * | |
| 61 * The [withCredentials] parameter specified that credentials such as a cookie | |
| 62 * (already) set in the header or | |
| 63 * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2) | |
| 64 * should be specified for the request. Details to keep in mind when using | |
| 65 * credentials: | |
| 66 * | 52 * |
| 67 * * Using credentials is only useful for cross-origin requests. | 53 * * Using credentials is only useful for cross-origin requests. |
| 68 * * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildca
rd (*). | 54 * * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildca
rd (*). |
| 69 * * The `Access-Control-Allow-Credentials` header of `url` must be set to tru
e. | 55 * * The `Access-Control-Allow-Credentials` header of `url` must be set to tru
e. |
| 70 * * If `Access-Control-Expose-Headers` has not been set to true, only a subse
t of all the response headers will be returned when calling [getAllRequestHeader
s]. | 56 * * If `Access-Control-Expose-Headers` has not been set to true, only a subse
t of all the response headers will be returned when calling [getAllRequestHeader
s]. |
| 71 * | 57 * |
| 72 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). | 58 * See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access
_authentication). |
| 73 */ | 59 */ |
| 74 static Future<HttpRequest> request(String url, | 60 factory $CLASSNAME.getWithCredentials(String url, |
| 75 {String method, bool withCredentials, String responseType, sendData, | 61 onComplete($CLASSNAME request)) => |
| 76 void onProgress(ProgressEvent e)}) { | 62 _HttpRequestUtils.get(url, onComplete, true); |
| 77 var completer = new Completer<String>(); | |
| 78 | |
| 79 var xhr = new HttpRequest(); | |
| 80 if (method == null) { | |
| 81 method = 'GET'; | |
| 82 } | |
| 83 xhr.open(method, url, true); | |
| 84 | |
| 85 if (withCredentials != null) { | |
| 86 xhr.withCredentials = withCredentials; | |
| 87 } | |
| 88 | |
| 89 if (responseType != null) { | |
| 90 xhr.responseType = responseType; | |
| 91 } | |
| 92 | |
| 93 if (onProgress != null) { | |
| 94 xhr.onProgress.listen(onProgress); | |
| 95 } | |
| 96 | |
| 97 xhr.onLoad.listen((e) { | |
| 98 if (xhr.status >= 200 && xhr.status < 300 || | |
| 99 xhr.status == 304 ) { | |
| 100 completer.complete(xhr); | |
| 101 } else { | |
| 102 completer.completeError(e); | |
| 103 } | |
| 104 }); | |
| 105 | |
| 106 xhr.onError.listen((e) { | |
| 107 completer.completeError(e); | |
| 108 }); | |
| 109 | |
| 110 if (sendData != null) { | |
| 111 xhr.send(sendData); | |
| 112 } else { | |
| 113 xhr.send(); | |
| 114 } | |
| 115 | |
| 116 return completer.future; | |
| 117 } | |
| 118 | 63 |
| 119 $!MEMBERS | 64 $!MEMBERS |
| 120 } | 65 } |
| OLD | NEW |